Replies: 1 comment 12 replies
-
To reshape the reactivity of your signals, you got import { createSignal, createEffect, createMemo } from "solid-js";
import { render } from "solid-js/web";
const [get, set] = createSignal(0);
const memo = createMemo(() => get() === 6 ? 7 : get());
createEffect(() => {
console.log("Executed", memo());
});
render(
() => <button onClick={() => set((x) => x + 1)}>Add</button>,
document.getElementById("app")!,
); There are also a few more reactive helpers in https://primitives.solidjs.community/, so if there are any more pieces you are missing, you might be able to find them there. |
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's suppose I have this very simple application
Each time I click the button, "Executed" will be logged followed by the current number, this is all optimal.
Now let's suppose that now for some reason my applications requires the number
7
to be skipped inside that effect, I could change theonClick
event like soBut for the semplicity of the argument let's say that I have no control over that handler or that it is not logically correct for my specific application to change the logic of said handler.
I could achieve the same result through the effect by doing this
But this has one side effect: The execution of
set()
will cause the current effect to be invalidated, sinceget()
is one of its dependencies, so it will be executed two times and the console will logInstead of
Moreover, this is not needed since I ensure to mutate the
value
variable after I increase theSignal
, thus already considering the side effects of my special case.This could be fixed by a new primitive:
assumeConsidered()
It would assume that the side effects of ONLY what's inside the provided function will be assumed to be already handled, this will also ensure that the presence of something that wasn't handled preventively would still trigger the second execution
Since I needed this functionality, I already made an implementation myself, and I thought it would have benefitted the whole ecosystem if used correctly
The reason it can also accepts the computation as an argument is because, in my codebase, I needed to both execute
untrack()
andassumeConsidered()
.I could've done it like this
But I wanted to reduce the "ignored" area as much as possible
Unfortunately I can't do that because
untrack()
removes the listener, so I did thisBeta Was this translation helpful? Give feedback.
All reactions