reductive icon indicating copy to clipboard operation
reductive copied to clipboard

Using ref instead of mutable in store record

Open MargaretKrutikova opened this issue 5 years ago • 3 comments

According to reason-react docs instance variables should be stored in state and be marked with ref instead of mutable to prepare for concurrent react.

I am unsure whether this makes any difference for a global state management solution like reductive, but would be nice to have a better insight into this from someone who has a better understanding of the internals of reductive and reason-react. If it does make a difference, the fix is rather simple but nice to have to be ready for the future changes.

Would appreciate any opinions/thoughts on this :)

MargaretKrutikova avatar Mar 02 '19 17:03 MargaretKrutikova

the ref type is roughly:

type ref('a) = {
  mutable contents: 'a
};

so that wouldn't change much conceptually 😊

bloodyowl avatar Mar 03 '19 15:03 bloodyowl

hm... yes, that makes sense. However, the docs I linked show an example in reasonml playground that demonstrates the difference when reading from state from a closure. Then it seems to work incorrectly if using mutable since the data might get stale by the time it is accessed. So it seems like there might be a risk of having the same problem in reductive..

Please correct me if I am misunderstanding something!

MargaretKrutikova avatar Mar 03 '19 18:03 MargaretKrutikova

For the function api I think preferring useRef where possible makes sense. For the old API yes, you must use a ref inside of state instead of a mutable field. Is there a scenario in the function api where useRef doesn't make sense?

rickyvetter avatar Sep 30 '19 17:09 rickyvetter