raveclassic/frp-ts

[QUESTION] There are no reactions in the react component

sagroone opened this issue · 2 comments

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

Hey @sagroone!

Sadly, this is just how React works:

  1. first, you create an atom right in the body of the component - const counter$ = newAtom(0)
  2. next, you subscribe to changes in that atom with useProperty hook
  3. 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), []);

@raveclassic Thanks for help