Skip to content

Instantly share code, notes, and snippets.

@naviocean
Created August 9, 2017 09:44
Show Gist options
  • Save naviocean/00fe385c32beadc87b273b47e49f7801 to your computer and use it in GitHub Desktop.
Save naviocean/00fe385c32beadc87b273b47e49f7801 to your computer and use it in GitHub Desktop.
Delay Render Until Rehydration Complete
// configureStore.js
import { createStore, applyMiddleware, compose } from 'redux';
import { autoRehydrate, persistStore } from 'redux-persist';
import { localStorage } from 'redux-persist/storages';
import { rootReducer } from '../reducers';
export default function configureStore() {
// use desired middlewares
const middlewares = [];
return new Promise((resolve, reject) => {
try {
const store = createStore(
rootReducer,
undefined,
compose(
autoRehydrate(),
applyMiddleware(...middlewares),
),
);
persistStore(
store,
{ storage: localStorage },
() => resolve(store)
);
} catch (e) {
reject(e);
}
});
}
// application entry file
import React, { Component } from 'react'
import { Provider } from 'react-redux';
import configureStore from './configureStore';
class Bootloader extends Component {
state = {
store: null
}
async componentWillMount () {
const store = await configureStore();
this.setState({ store });
}
render () {
if (this.state.store === null) {
return (
<Text>
Booting...
</Text>
)
}
return (
<Provider store={this.state.store}>
<App />
</Provider>
)
}
}
export default Bootloader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment