基礎(chǔ)使用
牽扯到 persistStore
persistReducer
兩個(gè) API荤堪。
// configureStore.js
import { createStore } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web
import rootReducer from './reducers'
const persistConfig = {
key: 'root',
storage,
}
const persistedReducer = persistReducer(persistConfig, rootReducer)
export default () => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return { store, persistor }
}
persistStore 用來生產(chǎn) persistor 的 store,是必須配置的。 persistReducer 不是必須的利职,而且可以對(duì)包含任意 reducer ,實(shí)現(xiàn)精確控制需要 persist 的內(nèi)容嗜湃。
import { PersistGate } from 'redux-persist/integration/react'
// ... normal setup, create store and persistor, import components etc.
const App = () => {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<RootComponent />
</PersistGate>
</Provider>
);
};
以上是 react app 的使用實(shí)例戳吝,