Skip to content

Instantly share code, notes, and snippets.

@Morriz
Last active January 13, 2023 13:20
Show Gist options
  • Save Morriz/9f1e23b368331c057dea6f9ae63a00ee to your computer and use it in GitHub Desktop.
Save Morriz/9f1e23b368331c057dea6f9ae63a00ee to your computer and use it in GitHub Desktop.
import { PersistGate } from 'redux-persist/integration/react'
import { configureStore, checkVersions } from '@store/store'
...
const { store, persistor } = configureStore()
export default class App extends Component {
...
render() {
const { hideSplash } = this.props
return (
<Provider store={store}>
<PersistGate persistor={persistor} onBeforeLift={checkVersions}>
<AppContainer hideSplash={hideSplash} uriPrefix="..." onNavigationStateChange={handleNavigationChange} />
</PersistGate>
</Provider>
)
}
...
import { combineReducers } from 'redux'
import { reducer as form } from 'redux-form'
import { persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
...
import SettingsReducer from '@modules/settings/SettingsState'
const persistedSettingsReducer = persistReducer(
{
key: 'settings',
storage,
},
SettingsReducer
)
const combinedReducers = combineReducers({
settings: persistedSettingsReducer,
...
form,
})
const persistedCombinedReducer = persistReducer(
{
key: 'primary',
storage,
blacklist: ['settings'],
},
combinedReducers
)
export default persistedCombinedReducer
import { applyMiddleware, createStore, compose } from 'redux'
import { persistStore } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import { setJSExceptionHandler, getJSExceptionHandler } from 'react-native-exception-handler'
import { setStore } from '@services/store'
import semverUtils from 'semver-utils'
import * as pkg from '../../package.json'
import middleware from './middleware'
import reducer from './reducer'
const enhancer = applyMiddleware(...middleware)
const previousExceptionHandler = getJSExceptionHandler()
// purge most from the store on fatal, but keep settings
setJSExceptionHandler((e, isFatal) => {
if (isFatal) storage.removeItem('persist:primary')
console.log('FATAL: ', e)
previousExceptionHandler(e, isFatal)
}, true)
// storage.removeItem('persist:settings')
let store
export async function checkVersions() {
const thisSemver = semverUtils.parse(pkg.version)
const prevVersion = store.getState().settings.version
console.log('prev app version: ', prevVersion)
console.log('this app version: ', pkg.version)
const prevSemver = prevVersion ? semverUtils.parse(prevVersion) : undefined
if (!prevVersion || thisSemver.major > prevSemver.major || thisSemver.minor > prevSemver.minor) {
console.log('breaking changes: clearing app state')
await storage.removeItem('persist:primary')
}
}
export function configureStore(initialState = {}) {
store = createStore(reducer, initialState, enhancer)
const persistor = persistStore(store)
// keep in registry
setStore(store)
if (module.hot) {
module.hot.accept(() => {
const nextRootReducer = require('./reducer').default
store.replaceReducer(nextRootReducer)
})
}
return { store, persistor }
}
@Morriz
Copy link
Author

Morriz commented Feb 2, 2019

When using redux-persist this is a clean way to check if any breaking changes have been published, to purge state before loading the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment