I'd like to run some code in my component, but only once.
I cannot put it in dependency-less
createEffect
, since it is supposed to run some time after the component is first rendered, but it must never be run more than once during the component lifecycle. I thought I'd use a local Boolean variable for that, like this:
(incidentally, this is code generated by an LLM, which was basically identical to what I've done), but it doesn't work – the effect still runs more than once. What am I doing wrong?4 Replies
sounds like you are looking for createReaction
createReaction -
Documentation for SolidJS, the signals-powered UI framework
i would do something like
createReaction(on(dataLoaded, () => ...))
just to be sure it's only the dataLoaded
that triggers it
to
technically, your Effect may run more than once , with any change to
but if you guarded the logic that need to run once with the boolean it should not have "side effects" and would work as if the effect didn't run in the fix line, checking the boolean first and short circuting it will not call the
dataLoaded()
but if you guarded the logic that need to run once with the boolean it should not have "side effects" and would work as if the effect didn't run in the fix line, checking the boolean first and short circuting it will not call the
dataLoaded()
and the effect should just "die"
because it will observe nothing.
you can make it more explicit but returning early when the flag is true, first thing in the effect body.
createReaction
is also a good option as suggestedThat behavior will also be default with run once Suspense in Solid 2.0