preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.63k stars 88 forks source link

Do signal effects support clean up functions? #495

Closed gabrielcoronel closed 5 months ago

gabrielcoronel commented 5 months ago

What I mean by "clean up functions" is a signal analog to traditional React's useEffect clean up functions. For example, with useEffect one can set a clean up function for an update effect, this function will run right before the next update, "undoing" ("cleaning up") such effect:

useEffect(() => {
  Chat.connect(username)

  // Clean up function
  return () => Chat.disconnect(username)
}, [username])

Do signal effects (useSignalEffect) support this feature? If so, how? If not, what is signals' alternative for "cleaning up" effects?

I've read through the documentation and I haven't find anything similar so far.

XantreDev commented 5 months ago

You can return function from effect and it will run when effect dependencies changed

gabrielcoronel commented 5 months ago

You can return function from effect and it will run when effect dependencies changed

Ok, I didn't know that. Is that in any official documentation you could me point me to?

XantreDev commented 5 months ago

Seems to be there is no info in docs about it but, it's obvious from types

XantreDev commented 5 months ago

I think we should add info about it

gabrielcoronel commented 5 months ago

I submitted a pull request to the documentation repository including information about signals