Skip to content

Recursion with AtomEffects #16

Closed Answered by dmaskasky
pengx17 asked this question in Q&A
Nov 3, 2023 · 4 comments · 4 replies
Discussion options

You must be logged in to vote

This is possible, and should be implemented with an abort controller so that you can cancel the current run on cleanup. This is only possible by writing to a writable atom and delaying a microtask inside the writable atom. This effectively bypasses the infinite loop check.

Here is an example of how to do this.
https://codesandbox.io/s/nifty-julien-d3xgsy

Alternatively, you could use a while loop inside atomEffect so that it never completes unless something causes it to rerun. This strategy is more readable because it doesn't need to bypass the infinite loop check. This is because the atomEffect is not listening to the atoms it is changing, so there is no infinite read loop.

Here is an exa…

Replies: 4 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@QWu4xYV
Comment options

Answer selected by pengx17
Comment options

You must be logged in to vote
3 replies
@dmaskasky
Comment options

@dmaskasky
Comment options

@QWu4xYV
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants