signals icon indicating copy to clipboard operation
signals copied to clipboard

effect is run during SSR (React version)

Open verekia opened this issue 3 years ago • 1 comments

Having effect(() => console.log('hello')) in a component prints in the console of a Next.js project, so it's being run on the server.

Is it a bug? If not, how to use effect on the client-only? (which is what most people would want I think).

verekia avatar Sep 09 '22 04:09 verekia

which is what most people would want I think

Depends on what you do with signals. I have a few small private projects/scripts which are node-only and don't require a framework. There effects need to fire.

That said we definitely need to provide a way to only run effects on the client. Need to think a bit more about this on how best to do that without requiring users to change their code.

A workaround for now is to instantiate effect() inside useEffect():

useEffect(() => {
  const dispose = effect(() => ...)
  return () => dispose();
}, [])

marvinhagemeister avatar Sep 09 '22 09:09 marvinhagemeister