Skip to content
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

Add render prop support to ClientOnly #49

Merged
merged 3 commits into from
Mar 3, 2022

Conversation

rossipedia
Copy link
Contributor

Deprecates non-render prop usage of children to avoid issues with using client-only imports on the server.

For example, this will cause a problem on the server due to how Remix replaces client only imports with an empty module:

import ClientOnly from "remix-utils";
import Chart from "Chart.client.tsx";

function FakeChart() { ... }

function ClientChart() {
  return (
    <ClientOnly fallback={<FakeChart />}>
      <Chart />
    </ClientOnly>
  );
}

Will give the error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function...

Moving to a render function sidesteps the issue by delaying the call to jsx or createElement until the hydrated component runs on the client:

import ClientOnly from "remix-utils";
import Chart from "Chart.client.tsx";

function FakeChart() { ... }

function ClientChart() {
  return (
    <ClientOnly fallback={<FakeChart />}>
      {() => <Chart />}
    </ClientOnly>
  );
}

Deprecates non-render prop usage of `children` to avoid issues with
create client-only imports on the server.

For example, this will cause a problem on the server due to how Remix
replaces client only imports with an empty module:

```ts
import ClientOnly from "remix-utils";
import Chart from "Chart.client.tsx";

function ClientChart() {
  return (
    <ClientOnly fallback={<FakeChart />}>
      <Chart />
    </ClientOnly>
  );
}
```

Moving to a render function sidesteps the issue by delaying the call to
`jsx` or `createElement` until the hydrated component runs on the
client:

```ts
import ClientOnly from "remix-utils";
import Chart from "Chart.client.tsx";

function ClientChart() {
  return (
    <ClientOnly fallback={<FakeChart />}>
      {() => <Chart />}
    </ClientOnly>
  );
}
```
@sergiodxa sergiodxa added the enhancement New feature or request label Mar 3, 2022
@sergiodxa sergiodxa self-requested a review March 3, 2022 18:06
Looks like the editor is not showing the deprecation message, probably because it's JSX
@sergiodxa sergiodxa merged commit 1d9c542 into sergiodxa:main Mar 3, 2022
@rossipedia rossipedia deleted the client-only-render-prop branch March 3, 2022 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants