-
Notifications
You must be signed in to change notification settings - Fork 46.4k
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
Simplify nested hook warning logic in dev #26238
base: main
Are you sure you want to change the base?
Conversation
We have so many dispatchers. And with my changes in facebook#26232 it's a bit harder to see if the previous logic was correct. Here I'm moving the flag to a separate value which makes it easier to see that it will work properly with the live dispatcher switch and that the prod behavior is not affected. I cheated and mutated ContextOnlyDispatcher in place in dev (previously ContextOnlyDispatcher.readContext would not warn when in a state update but that dispatcher wouldn't be active at the times it's relevant). Also adds the hook ordering warnings to useMemoCache.
|
||
ContextOnlyDispatcher.readContext = function <T>( | ||
context: ReactContext<T>, | ||
): T { | ||
warnInvalidContextAccess(); | ||
return readContext(context); |
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.
Cannot assign function to
ContextOnlyDispatcher.readContext
because propertyreadContext
is not writable.
One of the ways to tackle this will be to declare warnIfDevInvalidContextAccess
fn such that:
- it'll behave as warn-IfDev-InvalidContextAccess
- which may use
__DEV__
(?) on top of current behaviour ofwarnInvalidContextAccess
- and to declare it in the adjacent scope of
ContextOnlyDispatcher
and declaring ContextOnlyDispatcher
as:
export const ContextOnlyDispatcher: Dispatcher = {
readContext: function <T>(
context: ReactContext<T>,
): T {
warnIfDevInvalidContextAccess();// the above refactored fn
return readContext(context);
},
...,
}
@acdlite thoughts on this PR? (getting rid of the InvalidNestedHooksDispatcherOn{Mount,Update,Rerender}InDEV dispatchers in favor of tracking a separate flag that the other dev dispatchers read) |
We have so many dispatchers. And with my changes in #26232 it's a bit harder to see if the previous logic was correct. Here I'm moving the flag to a separate value which makes it easier to see that it will work properly with the live dispatcher switch and that the prod behavior is not affected.
I cheated and mutated ContextOnlyDispatcher in place in dev (previously ContextOnlyDispatcher.readContext would not warn when in a state update but that dispatcher wouldn't be active at the times it's relevant).
Also adds the hook ordering warnings to useMemoCache.