Skip to content

Ref pattern with jotai works well but...is this a bad idea?? #283

Closed Answered by dai-shi
tomhicks asked this question in Show and tell
Discussion options

You must be logged in to vote

Side effect in read function is not recommended, just like React render function.
(It may work in Legacy Mode, but in Concurrent Mode, it would behave unexpectedly.)

Having a mutable source in an atom is possible for expert users.

const scaleRefAtom = atom({ current: 1 })

But, the above is not very good as the ref is shared among Providers.

const scaleRefAtom = atom(() => ({ current: 1 }))

This is better, but still not recommended for ordinary users.


A jotai-preferred way is to read atom values in write.
If the final destination was an atom, it would be just easy.
If you need to setState from React.useState, it will be tricky.

function Item({x, y}: {x: number, y: number}) {
  // const sc…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Aslemammad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants