mobxjs / mobx-react

React bindings for MobX
https://mobx.js.org/react-integration.html
MIT License
4.85k stars 350 forks source link

Have react manage state #866

Closed egluhbegovic closed 4 years ago

egluhbegovic commented 4 years ago

Hey there!

Really enjoy MobX and how it has simplified the state management of our many apps.

I would like to know if there is a way to have MobX update the react store and then simply use react context to pass the state across the state tree.

At the moment I am doing this BUT, I need to wrap every component in observable in order for it to receive the updates.

I feel it should be possible that MobX runs a callback function that is passed to it and that takes the latest MobX state and updates the react app state.

That way MobX can be contained to one central module of the application and react does the rest, no need to worry about MobX downstream.

Let me know your thoughts around this and if it is possible.

Regards

danielkcz commented 4 years ago

Can you please set up a minimal reproduction with eg. CodeSandbox so it's more clear what you are talking about? It's fairly confusing what you are trying to achieve.

urugator commented 4 years ago

Mobx without observers doesn't make sense. If you simply want to invalidate the root component whenever the state changes you don't need mobx. If you want to use react to propagate the state from root to the rest of the tree, then the state must be immutable (considering you don't want every single component to update every single time a state is updated - without immutability there is no way for context/memo to detect change and possibly bail out from update) If you don't want to deal with immutability on your own, take a look at immer.

egluhbegovic commented 4 years ago

@FredyC Will give a sandbox example in next day or 2. @urugator I think I am being misunderstood as @FredyC said, let me provide a sandbox for clarity.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.