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.

How to have global state with recat-native-navigration

See original GitHub issue

@dai-shi Thanks for this repo. On RNN each screen is a seperate tree, and so you need to which has it’s own Provider. Like so:

Navigation.registerComponent(
    'Templates',
    () => props => (
      <Provider>
         <Templates {...props} />
      </Provider>
    ),
    () => Templates,
  );
  Navigation.registerComponent(
    'Editor',
    () => props => (
      <Provider>
         <Editor {...props} />
      </Provider>
    ),
    () => Editor,
  );

The problem is that each screen seem to have it’s own context or state, how can I make sure the state is shared across screens?

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Maxooscommented, Jul 30, 2020

I tried zustand and it worked like a charm! react-hooks-global-state looks good too, I may use it too in the future. Thanks for all the help!

0reactions
dai-shicommented, Jul 30, 2020

That workaround in the issue is a bit hacky. a) you don’t need Proxy to make it work. b) it may only work because another screen rerenders with an updated value in the mutable global variable (if it works, it’s because RNN behaves like that). c) you can’t use function update. In short, we don’t need to re-invent the context api for this.

I can easily use useState from individual components and have them stored in a global state.

If this is the goal, I’d encourage you to try https://github.com/dai-shi/react-hooks-global-state It will work like a charm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

State persistence | React Navigation
To be able to persist the navigation state, we can use the onStateChange and initialState props of the container. onStateChange - This prop...
Read more >
Storing global state in react-native/react-navigation app?
I have a react native/react navigation app with two screens. Before I implemented react-navigation I had a overarching App component that ...
Read more >
Using React Global State with Hooks and Context | Savas Labs
At the top of the component tree, ie in App.js, define global variables, and the Hooks to update their state. // App.js import ......
Read more >
How to manage your global state in a React app? | Medium
What options do I have to manage my React app state? ... For example, React Router for web apps or React Navigation for...
Read more >
Storing global state in react-native/react-navigation app?
Related Query · Sharing state across React Native app with React Navigation · React Native Android, several apps instances running at the same...
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