File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -16,17 +16,20 @@ enum Source {
16
16
type ValueRecord < T > = [ T , Source , T ] ;
17
17
18
18
const useUpdateEffect : typeof React . useEffect = ( callback , deps ) => {
19
- const [ firstMount , setFirstMount ] = React . useState ( true ) ;
19
+ const firstMountRef = React . useRef ( true ) ;
20
20
21
21
useLayoutEffect ( ( ) => {
22
- if ( ! firstMount ) {
22
+ if ( ! firstMountRef . current ) {
23
23
return callback ( ) ;
24
24
}
25
25
} , deps ) ;
26
26
27
27
// We tell react that first mount has passed
28
28
useLayoutEffect ( ( ) => {
29
- setFirstMount ( false ) ;
29
+ firstMountRef . current = false ;
30
+ return ( ) => {
31
+ firstMountRef . current = true ;
32
+ } ;
30
33
} , [ ] ) ;
31
34
} ;
32
35
Original file line number Diff line number Diff line change @@ -281,6 +281,19 @@ describe('hooks', () => {
281
281
282
282
expect ( container . textContent ) . toBe ( '1' ) ;
283
283
} ) ;
284
+
285
+ it ( 'render once' , ( ) => {
286
+ let count = 0 ;
287
+
288
+ const Demo = ( ) => {
289
+ const [ ] = useMergedState ( ) ;
290
+ count += 1 ;
291
+ return null ;
292
+ } ;
293
+
294
+ render ( < Demo /> ) ;
295
+ expect ( count ) . toBe ( 1 ) ;
296
+ } ) ;
284
297
} ) ;
285
298
286
299
describe ( 'useLayoutEffect' , ( ) => {
You can’t perform that action at this time.
0 commit comments