-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to mount dispatcher after use() when needed #26232
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -658,10 +658,6 @@ export function replaySuspendedComponentWithHooks<Props, SecondArg>( | |||||||||||
// only get reset when the component either completes (finishRenderingHooks) | ||||||||||||
// or unwinds (resetHooksOnUnwind). | ||||||||||||
if (__DEV__) { | ||||||||||||
hookTypesDev = | ||||||||||||
current !== null | ||||||||||||
? ((current._debugHookTypes: any): Array<HookType>) | ||||||||||||
: null; | ||||||||||||
hookTypesUpdateIndexDev = -1; | ||||||||||||
// Used for hot reloading: | ||||||||||||
ignorePreviousDependencies = | ||||||||||||
|
@@ -696,8 +692,13 @@ function renderWithHooksAgain<Props, SecondArg>( | |||||||||||
let numberOfReRenders: number = 0; | ||||||||||||
let children; | ||||||||||||
do { | ||||||||||||
didScheduleRenderPhaseUpdateDuringThisPass = false; | ||||||||||||
if (didScheduleRenderPhaseUpdateDuringThisPass) { | ||||||||||||
// It's possible that a use() value depended on a state that was updated in | ||||||||||||
// this rerender, so we need to watch for different thenables this time. | ||||||||||||
thenableState = null; | ||||||||||||
} | ||||||||||||
thenableIndexCounter = 0; | ||||||||||||
didScheduleRenderPhaseUpdateDuringThisPass = false; | ||||||||||||
|
||||||||||||
if (numberOfReRenders >= RE_RENDER_LIMIT) { | ||||||||||||
throw new Error( | ||||||||||||
|
@@ -841,8 +842,7 @@ function updateWorkInProgressHook(): Hook { | |||||||||||
// This function is used both for updates and for re-renders triggered by a | ||||||||||||
// render phase update. It assumes there is either a current hook we can | ||||||||||||
// clone, or a work-in-progress hook from a previous render pass that we can | ||||||||||||
// use as a base. When we reach the end of the base list, we must switch to | ||||||||||||
// the dispatcher used for mounts. | ||||||||||||
// use as a base. | ||||||||||||
let nextCurrentHook: null | Hook; | ||||||||||||
if (currentHook === null) { | ||||||||||||
const current = currentlyRenderingFiber.alternate; | ||||||||||||
|
@@ -876,16 +876,10 @@ function updateWorkInProgressHook(): Hook { | |||||||||||
if (currentFiber === null) { | ||||||||||||
// This is the initial render. This branch is reached when the component | ||||||||||||
// suspends, resumes, then renders an additional hook. | ||||||||||||
const newHook: Hook = { | ||||||||||||
memoizedState: null, | ||||||||||||
|
||||||||||||
baseState: null, | ||||||||||||
baseQueue: null, | ||||||||||||
queue: null, | ||||||||||||
|
||||||||||||
next: null, | ||||||||||||
}; | ||||||||||||
nextCurrentHook = newHook; | ||||||||||||
// Should never be reached because we should switch to the mount dispatcher first. | ||||||||||||
throw new Error( | ||||||||||||
'Update hook called on initial render. This is likely a bug in React. Please file an issue.', | ||||||||||||
); | ||||||||||||
} else { | ||||||||||||
// This is an update. We should always have a current hook. | ||||||||||||
throw new Error('Rendered more hooks than during the previous render.'); | ||||||||||||
|
@@ -951,7 +945,24 @@ function use<T>(usable: Usable<T>): T { | |||||||||||
if (thenableState === null) { | ||||||||||||
thenableState = createThenableState(); | ||||||||||||
} | ||||||||||||
return trackUsedThenable(thenableState, thenable, index); | ||||||||||||
const result = trackUsedThenable(thenableState, thenable, index); | ||||||||||||
if ( | ||||||||||||
currentlyRenderingFiber.alternate === null && | ||||||||||||
(workInProgressHook === null | ||||||||||||
? currentlyRenderingFiber.memoizedState === null | ||||||||||||
: workInProgressHook.next === null) | ||||||||||||
) { | ||||||||||||
// Initial render, and either this is the first time the component is | ||||||||||||
// called, or there were no Hooks called after this use() the previous | ||||||||||||
// time (perhaps because it threw). Subsequent Hook calls should use the | ||||||||||||
// mount dispatcher. | ||||||||||||
if (__DEV__) { | ||||||||||||
ReactCurrentDispatcher.current = HooksDispatcherOnMountInDEV; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a little trouble wrapping my head around whether it's necessary to do something with HooksDispatcherOnMountWithHookTypesInDEV here but my DebugValue test passes so ¯\_(ツ)_/¯? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That dispatcher is related to the hooks mismatch warning, not useDebugValue. But I think I recall the last time I looked at that code, it was due to a weird legacy mode only quirk related to React.lazy. We should try to clean it up (separately). Still, probably the worst thing that happens is we sometimes don't fire a warning where we should. The other weird case is the InvalidNestedHooksDispatcher(s). If we want to match production exactly then we have to account for those too. That's where you call a hook inside of e.g. useMemo. But that's considered undefined behavior so also not super critical. So I think we can merge as-is. Neither of those are as important as fixing the bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah sorry – HooksDispatcherOnMountWithHookTypesInDEV is because non-stateful hooks (like useDebugValue) can trigger the mismatch warning. Some of the tests in ReactHooks-test.internal using useContext or useDebugValue fail without it. But I couldn't get it to happen here with the test I added. Can you elaborate on what needs to be done with the InvalidNestedHooks ones? From a quick glance I would think we're already OK there and it will warn as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The react/packages/react-reconciler/src/ReactFiberHooks.js Lines 2972 to 2976 in 96cdeaf
So for example, if you were to call Although now that I type that out, maybe it just works because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meh. I agree it's undefined behavior and you get the first warning so I don't really care. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm although now is the dev and prod behavior different… |
||||||||||||
} else { | ||||||||||||
ReactCurrentDispatcher.current = HooksDispatcherOnMount; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
return result; | ||||||||||||
} else if ( | ||||||||||||
usable.$$typeof === REACT_CONTEXT_TYPE || | ||||||||||||
usable.$$typeof === REACT_SERVER_CONTEXT_TYPE | ||||||||||||
|
@@ -1998,6 +2009,7 @@ function updateEffectImpl( | |||||||||||
const nextDeps = deps === undefined ? null : deps; | ||||||||||||
let destroy = undefined; | ||||||||||||
|
||||||||||||
// currentHook is null when rerendering after a render phase state update. | ||||||||||||
if (currentHook !== null) { | ||||||||||||
const prevEffect = currentHook.memoizedState; | ||||||||||||
destroy = prevEffect.destroy; | ||||||||||||
|
@@ -2250,12 +2262,10 @@ function updateCallback<T>(callback: T, deps: Array<mixed> | void | null): T { | |||||||||||
const hook = updateWorkInProgressHook(); | ||||||||||||
const nextDeps = deps === undefined ? null : deps; | ||||||||||||
const prevState = hook.memoizedState; | ||||||||||||
if (prevState !== null) { | ||||||||||||
if (nextDeps !== null) { | ||||||||||||
const prevDeps: Array<mixed> | null = prevState[1]; | ||||||||||||
if (areHookInputsEqual(nextDeps, prevDeps)) { | ||||||||||||
return prevState[0]; | ||||||||||||
} | ||||||||||||
if (nextDeps !== null) { | ||||||||||||
const prevDeps: Array<mixed> | null = prevState[1]; | ||||||||||||
if (areHookInputsEqual(nextDeps, prevDeps)) { | ||||||||||||
return prevState[0]; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
hook.memoizedState = [callback, nextDeps]; | ||||||||||||
|
@@ -2283,13 +2293,11 @@ function updateMemo<T>( | |||||||||||
const hook = updateWorkInProgressHook(); | ||||||||||||
const nextDeps = deps === undefined ? null : deps; | ||||||||||||
const prevState = hook.memoizedState; | ||||||||||||
if (prevState !== null) { | ||||||||||||
// Assume these are defined. If they're not, areHookInputsEqual will warn. | ||||||||||||
if (nextDeps !== null) { | ||||||||||||
const prevDeps: Array<mixed> | null = prevState[1]; | ||||||||||||
if (areHookInputsEqual(nextDeps, prevDeps)) { | ||||||||||||
return prevState[0]; | ||||||||||||
} | ||||||||||||
// Assume these are defined. If they're not, areHookInputsEqual will warn. | ||||||||||||
if (nextDeps !== null) { | ||||||||||||
const prevDeps: Array<mixed> | null = prevState[1]; | ||||||||||||
if (areHookInputsEqual(nextDeps, prevDeps)) { | ||||||||||||
return prevState[0]; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
if (shouldDoubleInvokeUserFnsInHooksDEV) { | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could turn
nextWorkInProgressHook
into a module level variable and read from there. To avoid a tiny bit of duplication. And/or could splituse
into separate mount and update implementations. But since this is a fairly specialized branch it's fine this way too.