Global Feed Post Login
Replying to 8bd66a58...

const [countAtom, useCountListener] = atomWithListeners(0)

function EvenCounter() {

const [evenCount, setEvenCount] = useState(0)

useCountListener(

useCallback(

(get, set, newVal, prevVal) => {

// Every time `countAtom`'s value is set, we check if its new value

// is even, and if it is, we increment `evenCount`.

if (newVal % 2 === 0) {

setEvenCount((c) => c + 1)

}

},

[setEvenCount],

),

)

r

Avatar
M 10mo ago

01

Reply to this note

Please Login to reply.

Discussion

No replies yet.