raveclassic / frp-ts

Functional reactive values-over-time
MIT License
80 stars 8 forks source link

[QUESTION] There are no reactions in the react component #72

Closed sagroone closed 1 year ago

sagroone commented 1 year ago

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 1 year ago

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), []);

sagroone commented 1 year ago

@raveclassic Thanks for help