How to extract common reactive code?
I have some common code that I want to use in multiple components, like:
What is the correct way to refactor this code fragment to a separate function and use it from multiple components?
Can I just simply extract it as is, and return the
Signal
(or only the Accessor
in case of my example)?
E.g. what would happen if I call this from inside an effect?
Can I somehow prevent it to be called from an ordinary function?
Thanks.5 Replies
Yes, you can simply extract as is!
Can I somehow prevent it to be called from an ordinary function?Wdym with this?
Wdym with this?I mean that somehow prevent it to be called from a non-reactive context. Is there for example, some Typescript typing "trick" that can check it at compile-time? Or isn't it that simple? 🙂 Thanks, I created this cute function 🤩 And it seems to work 😄
No typing tricks. You can check if you are in a reactive context with
getListener
, but for ur hook it doesn't really matter too much.
If someone accesses it outside reactive context it will just not be reactive.
If you use eslint you will get a warning.
As long as u prefix the hook with use
or create
Thanks a lot for the detailed answer! 🙂
Ur super welcome!