Why is children()
a function in ClientOnly
?
#316
-
Hi 👋 Probably a silly question, but why in ClientOnly is If there's a link to React/Remix docs of the behavior that causes this decision, just that link would be great - I'll figure it out from there 👍 Thank you! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
When you write this <ClientOnly>
<Component />
</ClientOnly> This is converted to functions like this React.createElement(ClientOnly, {}, React.createElement(Component, {})) Even if not rendered, this React.createElement(ClientOnly, {
children: () => React.createElement(Component, {})
}) And with this, |
Beta Was this translation helpful? Give feedback.
When you write this
This is converted to functions like this
Even if not rendered, this
React.createElement(Component, {})
will be called on SSR and it will log a warning in the terminal saying that undefined is not a valid React component, by wrapping it in a function we're basically doing this:And with this,
React.createElement(Component, {})
will not run untilchildren
is executed, which will only happen client-side where Component is defined.