Skip to content

Commit caa69d6

Browse files
committed
Refactor host transitions to call useState directly
The useTransition hook is a thin wrapper around useState; all it really does it map over the return value. We going to change the return value for form actions, anyway, so we should call useState directly instead.
1 parent ccd7b3d commit caa69d6

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,11 @@ export function TransitionAwareHostComponent(): boolean {
776776
return false;
777777
}
778778
const dispatcher = ReactCurrentDispatcher.current;
779-
const [isPending] = dispatcher.useTransition();
780-
return isPending;
779+
const [booleanOrThenable] = dispatcher.useState();
780+
return typeof booleanOrThenable === 'boolean'
781+
? booleanOrThenable
782+
: // This will suspend until the async action scope has finished.
783+
useThenable(booleanOrThenable);
781784
}
782785

783786
export function checkDidRenderIdHook(): boolean {
@@ -2545,8 +2548,8 @@ export function startHostTransition<F>(
25452548
// it was stateful all along so we can reuse most of the implementation
25462549
// for function components and useTransition.
25472550
//
2548-
// Create the initial hooks used by useTransition. This is essentially an
2549-
// inlined version of mountTransition.
2551+
// Create the state hook used by TransitionAwareHostComponent. This is
2552+
// essentially an inlined version of mountState.
25502553
const queue: UpdateQueue<
25512554
Thenable<boolean> | boolean,
25522555
Thenable<boolean> | boolean,
@@ -2569,32 +2572,18 @@ export function startHostTransition<F>(
25692572
(dispatchSetState.bind(null, formFiber, queue): any);
25702573
setPending = queue.dispatch = dispatch;
25712574

2572-
// TODO: The only reason this second hook exists is to save a reference to
2573-
// the `dispatch` function. But we already store this on the state hook. So
2574-
// we can cheat and read it from there. Need to make this change to the
2575-
// regular `useTransition` implementation, too.
2576-
const transitionHook: Hook = {
2577-
memoizedState: dispatch,
2578-
baseState: null,
2579-
baseQueue: null,
2580-
queue: null,
2581-
next: null,
2582-
};
2583-
2584-
stateHook.next = transitionHook;
2585-
2586-
// Add the initial list of hooks to both fiber alternates. The idea is that
2587-
// the fiber had these hooks all along.
2575+
// Add the state hook to both fiber alternates. The idea is that the fiber
2576+
// had this hook all along.
25882577
formFiber.memoizedState = stateHook;
25892578
const alternate = formFiber.alternate;
25902579
if (alternate !== null) {
25912580
alternate.memoizedState = stateHook;
25922581
}
25932582
} else {
25942583
// This fiber was already upgraded to be stateful.
2595-
const transitionHook: Hook = formFiber.memoizedState.next;
2584+
const stateHook: Hook = formFiber.memoizedState;
25962585
const dispatch: (Thenable<boolean> | boolean) => void =
2597-
transitionHook.memoizedState;
2586+
stateHook.queue.dispatch;
25982587
setPending = dispatch;
25992588
}
26002589

0 commit comments

Comments
 (0)