[React Native] Sharing context/state with multiple screens (multiple roots, wix/react-native-navigation)
See original GitHub issueAs far as I understood, there has to be exactly one <RecoilRoot>
throughout the entire app, because only state under the same RecoilRoot
tree is shared. I’ve tried to use Recoil for my React Native app, but since I’m using wix/react-native-navigation, which uses separate roots for each Screen, I couldn’t manage to get Recoil working correctly, since state is only persisted on a per-screen basis.
I don’t have a lot of experience with state management libraries, but other libraries such as Redux or MobX are also compatible with react-native-navigation by simply providing a HoC that uses the store singleton, that’s why I’m creating this issue. I might also be doing something wrong, if so, please let me know.
Edit
I started digging around in the source code, and found out that the store
property for <RecoilRoot>
is actually available, but not exported (see this line, store_INTERNAL
). So what could potentially work is just a HoC function which exports a Root bound to the shared store and can therefore be used to create multiple screens (= roots) with the same store.
Edit 2
I also saw that the useRecoilBridgeAcrossReactRoots
relies on the React Context API (see this line), making it entirely incompatible with react-native-navigation (Source: wix/react-native-navigation docs: “Third Party React Context”). I’m guessing there is no workaround for that…
Edit 3
A react-native-navigation maintainer (@guyca) pointed out that they are actually sharing context from the React.Context API across screens, here’s how they do it:
If I’m not mistaken, this is essentially the same as what Recoil is already doing, but there’s one drawback: Re-renders aren’t triggered across screens when the context changes. Unfortunately I don’t know enough about the Context API to provide workarounds or even fixes/solutions for this, but if anyone has an idea please discuss here.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:12 (3 by maintainers)
Top GitHub Comments
@a-eid we are discussing about a possible solution to support multiple similar use cases.
I’m wondering if there is any plans to support / work around this ?