Reputation: 487
I'm not sure if I am using the getStoredState
from redux-persist library correctly. I'd like to use the store that has been persisted as the default redux store.
config/store.js
async function getState() {
const storedState = await getStoredState({ storage: AsyncStorage });
const store = createStore(reducers, storedState, compose(
applyMiddleware(logger),
autoRehydrate()
));
persistStore(store, {storage: AsyncStorage);
}
export default getState();
App.js
import store from 'config/store';
import { Provider } from 'react-redux';
export default () => (
<Provider store={store}>
//
</Provider>
)
This produces the following warnings:
Warning: Failed child context type: The child context `store.subscribe` is marked as required in `Provider`, but its value is `undefined`.
Warning: Failed prop type: The prop `store.subscribe` is marked as required in `Provider`, but its value is `undefined`
Any ideas what is causing these warnings? I have a feeling I've done something wrong in store.js
Upvotes: 3
Views: 2011
Reputation: 834
The problem is that when you are trying to instantiate the Provider your store variable is still pending: the getStoreState method returns a Promise, so you have to wait until it completes to use the store.
This example shows a sample implementation:
import {getStoredState, createPersistor} from 'redux-persist'
class App extends Component {
constructor(props) {
super(props)
this.store = null
this.persistor = null
}
componentWillMount(){
const persistConfig = {storage: AsyncStorage}
getStoredState(persistConfig, (err, restoredState) => {
this.store = createStore(
reducers,
restoredState,
compose(
applyMiddleware(logger,)
)
)
this.persistor = createPersistor(this.store, persistConfig)
// Now that we have the store setted, reload the component
this.forceUpdate()
})
}
render() {
if (!this.store) {
// Loading screen
return <Text>Loading</Text>
} else {
return (
<Provider store={this.store}>
<Router />
</Provider>
)
}
}
}
Any improvements tips are welcome.
Upvotes: 1