question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[React Native] Sharing context/state with multiple screens (multiple roots, wix/react-native-navigation)

See original GitHub issue

As 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…

Screenshot 2020-11-07 at 11 35 11

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:open
  • Created 3 years ago
  • Reactions:8
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mondaychencommented, Jan 10, 2021

@a-eid we are discussing about a possible solution to support multiple similar use cases.

2reactions
a-eidcommented, Jan 9, 2021

I’m wondering if there is any plans to support / work around this ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[React Native] Sharing context/state with multiple screens ...
Probably, create a dummy root somewhere and bridge from there to all screens (roots). Can you create an example with expo snack? 2...
Read more >
#2 Root Screen | Wix React Native Navigation 2 - YouTube
2 - We'll learn how to set our very first screen in React Native Navigation. ... # 2 Root Screen | Wix React...
Read more >
React Context API | React Native Navigation
You can use the React Context API with React Native Navigation with a limitation. ... If you need to trigger a re-render across...
Read more >
shengtaii/awesome-react-native - Gitee
react -native-snap-carousel ☆3959 - Swiper component for React Native with previews, multiple layouts, parallax images, performant handling of huge numbers ...
Read more >
How to use react native navigation wix with context to share ...
This should all work, however the caveat is if you register 2 screens with the same Context Provider (for example, Main as your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found