encapsulating logic that uses refs?
Hi, so I'm porting a react app that does something like so (eliding lots of annoying extra details):
There's a problem with the naive translation of this:
which is that unlike react, which will rerun the hook if ref changes, and normally only changes
ref.current
to prevent this, solid will run the effect one with the initial value, and completely not notice that the refA changed. because of pass-by-value, refB is a copy of the original binding to the original value, not a reference to the refA binding. what alternate patterns should I be trying instead? some off-the-cuff thoughts:
* put the ref into a signal ala ref={setSignal}
* split out the doSomething() parts of the hook and the createEffect() part of the hook. In this world by convention you aren't actually allowed to encapsulate createEffect() calls, only the contents of them (with any dependency injection you need to pass in signals or w/e)
* something else? bring back ref.current
?4 Replies
Use a signal for a ref
And pass the signal to the custom hook
createCustomEffect(signalRef)
Then in the hook, call refB
Inside the effect👍
yep, that worked! what I ended up doing ultimately was
doing the inline callback thing vs making a full signal is just a matter of preference I think, the signal itself won't ever change after initial mount
There could be cases where it does, for example, if you have a component with the ref in a
Show
the ref could change but in this case your callback wouldn't be reactive to refA
changing
so it would be pointing to something that no longer exists