Skip to content

Why is children() a function in ClientOnly? #316

Answered by sergiodxa
ilyapopovs asked this question in Q&A
Discussion options

You must be logged in to vote

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(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:

React.createElement(ClientOnly, {
  children: () => React.createElement(Component, {})
})

And with this, React.createElement(Component, {}) will not run until children is executed, which will only happen client-side where Component is defined.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ilyapopovs
Comment options

Answer selected by ilyapopovs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants