[QUESTION] There are no reactions in the react component
sagroone opened this issue · 2 comments
sagroone commented
Hi there!
I'm trying to use your lib with react, but there are no reactions in the component
Does need anything else for reaction?
And one more question, does this library work with React Native?
Sandbox link
https://codesandbox.io/s/dreamy-benz-5utty9?file=/src/App.js
raveclassic commented
Hey @sagroone!
Sadly, this is just how React works:
- first, you create an atom right in the body of the component -
const counter$ = newAtom(0)
- next, you subscribe to changes in that atom with
useProperty
hook - here's the trick, whenever atom's value changes,
useProperty
hook re-renders the whole component starting from scratch which ultimately re-creates the atom from pt.1
To fix this, you need to wrap the atom in useMemo
- const counter$ = useMemo(() => newAtom(0), []);
sagroone commented
@raveclassic Thanks for help