Skip to content

Instantly share code, notes, and snippets.

@Morriz
Last active January 13, 2023 13:20

Revisions

  1. Morriz revised this gist Feb 2, 2019. 2 changed files with 18 additions and 3 deletions.
    18 changes: 18 additions & 0 deletions AppNavigator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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>
    )
    }
    ...
    3 changes: 0 additions & 3 deletions store.js
    Original file line number Diff line number Diff line change
    @@ -34,9 +34,6 @@ export async function checkVersions() {

    export function configureStore(initialState = {}) {
    store = createStore(reducer, initialState, enhancer)
    const persistor = persistStore(store, undefined, () => {
    checkVersions(store) // this should go to 'PersistGate.onBeforeLift' once published
    })
    const persistor = persistStore(store)

    // keep in registry
  2. Morriz created this gist Feb 2, 2019.
    30 changes: 30 additions & 0 deletions reducer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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
    51 changes: 51 additions & 0 deletions store.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    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, undefined, () => {
    checkVersions(store) // this should go to 'PersistGate.onBeforeLift' once published
    })
    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 }
    }