Search code examples
reactjsmobxmobx-react

Using Mobx observables with React dependencies


Mobx docs says to use pattern useEffect(reaction(..)) to track observable changes, which looks like some cross-breeding to me. Whats the problem of using react dependencies array to achieve that? I did a basic test, and it works as intended:

const Hello = observer(() => {
  const {
    participantStore: { audioDisabled },
  } = useStores();
  useEffect(() => {
    console.log('changed', audioDisabled);
  }, [audioDisabled]);

  return <h1>TEST ME</h1>;
});

Solution

  • There is absolutely no problem with using React things like useEffect, useMemo and etc with MobX. You just need to list all dependencies and if there are many of them maybe it is easier to use reaction or autorun.

    So feel free to use whatever way you like more.