Skip to content

Is there a way to subscribe atom value change? #1319

Closed Answered by sixmen
sixmen asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, it will works. But countAtom & doneAtom are unrelated, so I want to avoid above pattern.

I thought an another example that resembles my current problem.

const rangeAtom = atom(5);
const currentAtom = atom(3);

const decRange = atom(null, (get, set) => {
  const r = get(rangeAtom) - 1;
  set(rangeAtom, r);
  if (get(currentAtom) >= r) {
    set(currentAtom, r-1);
  }
})

const App = () => {
  const range = useAtomValue(rangeAtom);
  const current = useAtomValue(currentAtom);
  const dec = useSetAtom(decRange);
  return <Provider>
    {current}
    <select value={current}>
      {new Array(range).fill(0).map((_v, i) =>
        <option value={i}>{i}</option>)}
    </select>
    <button …

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@dai-shi
Comment options

@sixmen
Comment options

Answer selected by sixmen
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@dai-shi
Comment options

@StringKe
Comment options

@StringKe
Comment options

@dai-shi
Comment options

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
Converted from issue

This discussion was converted from issue #1318 on August 01, 2022 03:11.