React-style effect cleanup? #1283
Replies: 3 comments 3 replies
-
This would conflict with a feature in createEffect that allows you to pass a value to subsequent calls: createEffect((iteration) => {
console.log(value(), iteration);
return iteration + 1;
}, 0) |
Beta Was this translation helpful? Give feedback.
-
If you really need it, you could still implement it yourself: const createReactLikeEffect = (fn) => {
createEffect(() => {
const cleanup = fn();
if (cleanup) onCleanup(cleanup);
});
}; |
Beta Was this translation helpful? Give feedback.
-
There's one other advantage, which is that you can do multiple cleanups. A common pattern for event listeners is for the |
Beta Was this translation helpful? Give feedback.
-
Could we have a more React-like API for effect cleanups?
So this:
Instead of this:
It would just make it a little easier to switch between frameworks.
And having cleanup built-into the
createEffect
contract makes sense, I think? Rather than having to search foronCleanup
and treating that as a separate thing, it would just be more close at hand.Beta Was this translation helpful? Give feedback.
All reactions