Problem with import react-json-view #1791
-
Good afternoon! I'm just trying to use a lib that works only on the client but I can't understand why it's giving error with the imports, sandbox with error reproduction: I tried to use the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
A couple of things
export default function Index() {
console.log(document); // won't work
useEffect(() => {
console.log(document); // works
}, []);
return <div>Hello</div>;
} This doesn't help with your use case though because it's an external dependency. Importing and re-exporting the component ( From my understanding, the component expects a JSON string for its const json = { a: 1 };
// needs to be
const json = JSON.stringify({ a: 1 }); The solution would be to find another component or raise an issue in its repository, asking the maintainers to make it SSR-compatible and using this workaround in the meantime. |
Beta Was this translation helpful? Give feedback.
A couple of things
react-json-view
referencesdocument
7 times. Doing so leads to errors when using SSR with React, like Remix does. SSR means that the JavaScript code is not (only) executed in a browser but also in non-browser environments like Node.js or others. There is nodocument
in non-browser environments.You can test this by adding a
console.log(document);
to any component, which won't work. We can use browser-only APIs by wrapping them in anuseEffect
as described by the docs.This doesn't help with your use c…