Skip to content

How to have global state with recat-native-navigration #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Maxoos opened this issue Jul 30, 2020 · 5 comments
Closed

How to have global state with recat-native-navigration #58

Maxoos opened this issue Jul 30, 2020 · 5 comments

Comments

@Maxoos
Copy link

Maxoos commented Jul 30, 2020

@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!

@dai-shi
Copy link
Owner

dai-shi commented Jul 30, 2020

Hey, thanks for trying.
I'm not very familiar with RNN, but from the doc https://wix.github.io/react-native-navigation/docs/third-party-react-context/
it's not possible. It's the limitation in RNN.

As the doc says, you need to find non-context solution. For example:

@dai-shi
Copy link
Owner

dai-shi commented Jul 30, 2020

Ah, if you like the state usage tracking feature in react-tracked, of course this:

@Maxoos
Copy link
Author

Maxoos commented Jul 30, 2020

Thanks for the links!

What I like about react-tracked is that I can easily use useState from individual components and have them stored in a global state.

I've been digging around a bit and it seems that you can do it with a Proxy

wix/react-native-navigation#4517 (comment)

But not too sure how to do it with this lib.

@dai-shi
Copy link
Owner

dai-shi commented 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.

@Maxoos
Copy link
Author

Maxoos commented 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!

@Maxoos Maxoos closed this as completed Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants