-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[Experimental] Add useInsertionEffect #21913
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 |
---|---|---|
|
@@ -385,6 +385,22 @@ function useRef<T>(initialValue: T): {|current: T|} { | |
} | ||
} | ||
|
||
function useInsertionEffect( | ||
create: () => mixed, | ||
inputs: Array<mixed> | void | null, | ||
) { | ||
if (__DEV__) { | ||
currentHookNameInDev = 'useInsertionEffect'; | ||
console.error( | ||
'useInsertionEffect does nothing on the server, because its effect cannot ' + | ||
"be encoded into the server renderer's output format. This will lead " + | ||
'to a mismatch between the initial, non-hydrated UI and the intended ' + | ||
'UI. To avoid this, useInsertionEffect should only be used in ' + | ||
'components that render exclusively on the client.', | ||
); | ||
} | ||
} | ||
Comment on lines
+388
to
+402
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. How are CSS-in-JS libraries supposed to handle SSR then? If the main use for this hook is stylesheet libraries then I think we need to document a compelling strategy for how those should be implemented "end to end". In Emotion we currently conditionally render Especially that last part of this error message seems to be off - given the primary use case for this hook. Style-based components cannot be exclusively rendered on the client because that would almost completely obliviate the sense of SSRing applications using those kinds of libraries. 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. @Andarist short term they'll insert them into the stream separately, chunk points. @sebmarkbage is writing up a tutorial for how to do that as a working group post. Long term, we have an RFC we're going to publish for how we intend to handle this in an integrated way that works isomorphically. 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. Interesting, thanks for the comment and I can't wait to read how this will work 👍 However, that probably means that the last paragraph of my comment is still valid - the error message here seems to be somewhat inaccurate and could throw somebody off. Unless you expect library maintainers to ignore that and "know better". 🤔 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. It's probably wrong in the same sense as the useLayoutEffect warning being wrong since it can also be done differently on the server to inject through different means and needs to be done conditionally to workaround these issues. |
||
|
||
export function useLayoutEffect( | ||
create: () => (() => void) | void, | ||
inputs: Array<mixed> | void | null, | ||
|
@@ -508,6 +524,7 @@ export const Dispatcher: DispatcherType = { | |
useReducer, | ||
useRef, | ||
useState, | ||
useInsertionEffect, | ||
useLayoutEffect, | ||
useCallback, | ||
// useImperativeHandle is not run in the server environment | ||
|
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.
Didn't we decide to drop the
ion
and just douseInsertEffect
? timberlake.gifThere is a legit noun.
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.
Yes, and then no.
useInsertEffect
sounds too much like the thing you're inserting is an effect, which I think we noticed when we started talking about it again and thought of it as a verb first (which is probably the natural reading of it).