-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Using unstated with RNN and auth #5081
Comments
Isn't it sufficient to just wrap your screen with Unstated Provider when you register them? Like below:
Each screen you register is treated as |
I tried that, but it didn't work at all. I got |
That was my own silly bug, fixed now (mental note, |
I would definitely look into redux. There are many examples of it working with React Native Navigation and it provides you with global state management like you want. |
Not sure why Unstated Provider has that behaviour. I use react-apollo, react-apollo-hooks and mobx which all provide their own Provider and I wrapped these for all screens that are being registered with RNN and never had any side effect as they keep them the same instance. Also I created mobx rootStore with React.createContext and use React.useContext to use it in Functional Component, but I guess it keeps the same "context" because it's mobx? Would you care to share a demo repo with RNN and Unstated setup? I'm keen to have a look. |
The problem is here: https://github.com/jamiebuilds/unstated/blob/master/src/unstated.js#L163 Since all the RNN Provider instances end up siblings, they don't share state. I fixed it by basically copying this library (it's small) and making them all use "parentMap" and passing in a Map to the initial createContext function. |
I had a quick play around with // RNN Provider to register each screen
import { Provider } from 'unstated'
class RNNHocProvider extends React.Component<Props> {
return (
<Provider>
<WrappedComponent />
</Provider>
)
}
// Unstated Counter Container
import { Container } from 'unstated'
class CounterContainer extends Container {
// snip
}
export const counterContainer = new CounterContainer()
// Your screen component 1
import { Subscribe } from 'unstated'
import { counterContainer } from 'path-to-your-counter-container'
const ScreenComp1 = () => (
<Subscribe to={[counterContainer]}>
{counter => <View />}
</Subscribe>
)
// Your screen component 2
import { Subscribe } from 'unstated'
import { counterContainer } from 'path-to-your-counter-container'
const ScreenComp2 = () => (
<Subscribe to={[counterContainer]}>
{counter => <View />}
</Subscribe>
) Upon updating counter state in |
Maybe this will help |
We use the issue tracker exclusively for bug reports and feature requests. This issue appears to be a general usage or support question. Instead, please ask a question on Stack Overflow with the |
(I would suggest it's a feature request, because I think the idea that there may need to be "a thing" at the top of the render hierarchy is not limited to unstated. Perhaps it's simply not possible with RNN, but if it is, then I'd argue it should be exposed) |
It's more of how you architecture your app, you can pass the same instance of the provider when you are registering your screen to avoid having multiple instances of Provider. Kinda like the |
@jinshin1013 have you tried Unstated-next? |
I had a brief play around, I had a success by passing the same instance of the Unstated Container to |
Issue Description
I am using unstated for state management which requires that I surround the "root" component with a Provider component (unstated's Provider, not RNN). Before RNN, I just put this around the App component. But the auth examples I've found for RNN talk about launching an auth flow and THEN navigating to the App component. That wouldn't work in this case because it would cause a new Provider to be spun up which has all sorts of bad side effects.
I can imagine a setRoot with a stack that had the auth on it that just moved from the Welcome/Splash screen directly to the tab-based navigation of the main app, but I'm not sure why the auth examples wouldn't have done that instead if it was that easy.
How does one integrate React-context based state into RNN without a JS-side root component that's always in the hierarchy?
Environment
The text was updated successfully, but these errors were encountered: