Help with client side components. #9819
antonysastre
started this conversation in
General
Replies: 2 comments 3 replies
-
You can't do like this. Because: import Hello from '../components/Hello.client';
export default function Home() {
return (
<div>
<Hello />
</div>
);
} When run in server, it will be: export default function Home() {
return (
<div>
<Hello />
</div>
);
} So And you can do this to solve this problem: import Hello from '../components/Hello.client';
const isClient = typeof document !== 'undefined';
export default function Home() {
return (
<div>
{isClient && <Hello />}
</div>
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
I recommend using import { ClientOnly } from 'remix-utils/client-only';
import Hello from '../components/Hello.client';
export default function Home() {
return (
<div>
<ClientOnly>{() => <Hello />}</ClientOnly>
</div>
);
} The ensures that the component renders after hydration so that you won't get the hydration errors. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
New to, and loving, Remix!
I'm a bit stuck on understanding "client side only" code. Here's my situation:
I'm getting this error when trying to import and use a component in a route.
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
app/routes/home.tsx
The error is on this file on the line.
app/components/hello.client.tsx
Am i missing how to do this properly?
The reason i'm using *.client.tsx on the component:
This code is just test code to sanity check the error.
The library that i'm using originally breaks on
self is undefined
— I think due to it using thewindow
object. I was hoping that forcing the component to client side would resolve that. But i get a sense i'm doing it wrong?Could anyone guide me here? 🙏 :slight_smile:
Beta Was this translation helpful? Give feedback.
All reactions