Next.js Discord

Discord Forum

How do I use a fs and useState in the same component

Answered
Gecco posted this in #help-forum
Open in Discord
Hello, I need to use useState in a component where I also need to use fs for file access. This seems to be impossible since fs is server online while useState is client only. Is there a way to use them both without having to create a wrapper component which isn't practical here. Thanks.
Answered by Giant panda
No there is no way to have them in the same component.
View full answer

5 Replies

Giant panda
No there is no way to have them in the same component.
Answer
Giant panda
One is running on the server the other on the client. It’s virtually impossible.
@Giant panda One is running on the server the other on the client. It’s virtually impossible.
Executing the relevant server sides componenets on the server then leaving the client side components to be rendered later would be an option but I'm going to assume the Next ecosystem would not acomodate. Regardless thanks for the answer.
Giant panda
There is nothing wrong loading the data in a server component and passing it down into a client component
Just make it a prop and consume it