@@ -61,7 +61,7 @@ type Effect = {
61
61
tag : HookEffectTag ,
62
62
create : ( ) => mixed ,
63
63
destroy : ( ( ) => mixed ) | null ,
64
- inputs : Array < mixed > ,
64
+ deps : Array < mixed > | null ,
65
65
next : Effect ,
66
66
} ;
67
67
@@ -471,12 +471,12 @@ export function useReducer<S, A>(
471
471
return [workInProgressHook.memoizedState, dispatch];
472
472
}
473
473
474
- function pushEffect ( tag , create , destroy , inputs ) {
474
+ function pushEffect ( tag , create , destroy , deps ) {
475
475
const effect : Effect = {
476
476
tag,
477
477
create,
478
478
destroy,
479
- inputs ,
479
+ deps ,
480
480
// Circular
481
481
next : ( null : any ) ,
482
482
} ;
@@ -516,34 +516,36 @@ export function useRef<T>(initialValue: T): {current: T} {
516
516
517
517
export function useLayoutEffect (
518
518
create : ( ) => mixed ,
519
- inputs : Array < mixed > | void | null,
519
+ deps : Array < mixed > | void | null,
520
520
): void {
521
- useEffectImpl ( UpdateEffect , UnmountMutation | MountLayout , create , inputs ) ;
521
+ useEffectImpl ( UpdateEffect , UnmountMutation | MountLayout , create , deps ) ;
522
522
}
523
523
524
524
export function useEffect(
525
525
create: () => mixed ,
526
- inputs : Array < mixed > | void | null,
526
+ deps : Array < mixed > | void | null,
527
527
): void {
528
528
useEffectImpl (
529
529
UpdateEffect | PassiveEffect ,
530
530
UnmountPassive | MountPassive ,
531
531
create ,
532
- inputs ,
532
+ deps ,
533
533
) ;
534
534
}
535
535
536
- function useEffectImpl(fiberEffectTag, hookEffectTag, create, inputs ): void {
536
+ function useEffectImpl(fiberEffectTag, hookEffectTag, create, deps ): void {
537
537
currentlyRenderingFiber = resolveCurrentlyRenderingFiber ( ) ;
538
538
workInProgressHook = createWorkInProgressHook ( ) ;
539
539
540
- let nextInputs = inputs !== undefined && inputs !== null ? inputs : [ create ] ;
540
+ const nextDeps = deps === undefined ? null : deps ;
541
541
let destroy = null ;
542
542
if ( currentHook !== null ) {
543
543
const prevEffect = currentHook . memoizedState ;
544
544
destroy = prevEffect . destroy ;
545
- if ( areHookInputsEqual ( nextInputs , prevEffect . inputs ) ) {
546
- pushEffect ( NoHookEffect , create , destroy , nextInputs ) ;
545
+ // Assume these are defined. If they're not, areHookInputsEqual will warn.
546
+ const prevDeps : Array < mixed > = ( prevEffect . deps : any ) ;
547
+ if ( nextDeps !== null && areHookInputsEqual ( nextDeps , prevDeps ) ) {
548
+ pushEffect ( NoHookEffect , create , destroy , nextDeps ) ;
547
549
return ;
548
550
}
549
551
}
@@ -553,20 +555,18 @@ function useEffectImpl(fiberEffectTag, hookEffectTag, create, inputs): void {
553
555
hookEffectTag ,
554
556
create ,
555
557
destroy ,
556
- nextInputs ,
558
+ nextDeps ,
557
559
) ;
558
560
}
559
561
560
562
export function useImperativeHandle< T > (
561
563
ref: { current : T | null } | ((inst: T | null) => mixed ) | null | void ,
562
564
create : ( ) => T ,
563
- inputs : Array < mixed > | void | null,
565
+ deps : Array < mixed > | void | null,
564
566
): void {
565
- // TODO: If inputs are provided, should we skip comparing the ref itself?
566
- const nextInputs =
567
- inputs !== null && inputs !== undefined
568
- ? inputs . concat ( [ ref ] )
569
- : [ ref , create ] ;
567
+ // TODO: If deps are provided, should we skip comparing the ref itself?
568
+ const nextDeps =
569
+ deps !== null && deps !== undefined ? deps . concat ( [ ref ] ) : [ ref ] ;
570
570
571
571
// TODO: I've implemented this on top of useEffect because it's almost the
572
572
// same thing, and it would require an equal amount of code. It doesn't seem
@@ -585,7 +585,7 @@ export function useImperativeHandle<T>(
585
585
refObject . current = null ;
586
586
} ;
587
587
}
588
- } , nextInputs ) ;
588
+ } , nextDeps ) ;
589
589
}
590
590
591
591
export function useDebugValue(
@@ -599,45 +599,45 @@ export function useDebugValue(
599
599
600
600
export function useCallback< T > (
601
601
callback: T,
602
- inputs : Array< mixed > | void | null,
602
+ deps : Array< mixed > | void | null,
603
603
): T {
604
604
currentlyRenderingFiber = resolveCurrentlyRenderingFiber ( ) ;
605
605
workInProgressHook = createWorkInProgressHook ( ) ;
606
606
607
- const nextInputs =
608
- inputs !== undefined && inputs !== null ? inputs : [ callback ] ;
607
+ const nextDeps = deps === undefined ? null : deps ;
609
608
610
609
const prevState = workInProgressHook . memoizedState ;
611
610
if ( prevState !== null ) {
612
- const prevInputs = prevState [ 1 ] ;
613
- if ( areHookInputsEqual ( nextInputs , prevInputs ) ) {
611
+ // Assume these are defined. If they're not, areHookInputsEqual will warn.
612
+ const prevDeps : Array < mixed > = prevState [ 1 ] ;
613
+ if ( nextDeps !== null && areHookInputsEqual ( nextDeps , prevDeps ) ) {
614
614
return prevState [ 0 ] ;
615
615
}
616
616
}
617
- workInProgressHook . memoizedState = [ callback , nextInputs ] ;
617
+ workInProgressHook . memoizedState = [ callback , nextDeps ] ;
618
618
return callback ;
619
619
}
620
620
621
621
export function useMemo< T > (
622
622
nextCreate: () => T ,
623
- inputs : Array < mixed > | void | null,
623
+ deps : Array < mixed > | void | null,
624
624
): T {
625
625
currentlyRenderingFiber = resolveCurrentlyRenderingFiber ( ) ;
626
626
workInProgressHook = createWorkInProgressHook ( ) ;
627
627
628
- const nextInputs =
629
- inputs !== undefined && inputs !== null ? inputs : [ nextCreate ] ;
628
+ const nextDeps = deps === undefined ? null : deps ;
630
629
631
630
const prevState = workInProgressHook . memoizedState ;
632
631
if ( prevState !== null ) {
633
- const prevInputs = prevState [ 1 ] ;
634
- if ( areHookInputsEqual ( nextInputs , prevInputs ) ) {
632
+ // Assume these are defined. If they're not, areHookInputsEqual will warn.
633
+ const prevDeps = prevState [ 1 ] ;
634
+ if ( nextDeps !== null && areHookInputsEqual ( nextDeps , prevDeps ) ) {
635
635
return prevState [ 0 ] ;
636
636
}
637
637
}
638
638
639
639
const nextValue = nextCreate ( ) ;
640
- workInProgressHook . memoizedState = [ nextValue , nextInputs ] ;
640
+ workInProgressHook . memoizedState = [ nextValue , nextDeps ] ;
641
641
return nextValue ;
642
642
}
643
643
0 commit comments