configure redux-persist according to the tutorial. It is true that redux data is stored in localstorage, but it is all initialized data. The code is posted below.
store.js
import { createStore } from "redux"
import { persistStore, persistReducer } from "redux-persist"
import storage from "redux-persist/lib/storage" // defaults to localStorage for web and AsyncStorage for react-native
import combineReducers from "./reducers.js"
const persistConfig = {
key: "root",
storage,
}
const persistedReducer = persistReducer(persistConfig, combineReducers)
export default () => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return { store, persistor }
}
outermost react component.
import React from "react"
import ReactDOM from "react-dom"
import "./styles/main.scss"
import {Provider} from "react-redux";
import storeConfig from "./redux/store"
const { persistor, store } = storeConfig()
import { PersistGate } from "redux-persist/integration/react"
// Store Initialization
// ------------------------------------
// Render Setup
// ------------------------------------
const MOUNT_NODE = document.getElementById("root")
let render = () => {
const App = require("./routes/App").default
ReactDOM.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
,
MOUNT_NODE
)
}
data stored in and out of log
localstorage
dedux dev tool
data can only be pulled and pushed once from localstorage during initialization.
when I actively write data to store, I do not write new data to localstorage, resulting in refresh, the data pulled from localstorage is unupdated data in localstorage
I try to change the data in localstorage, then refresh it, and find that the data I changed is in store again (that is, it is not always initialized data. )
all right, the case is solved. Famous Detective Conan time! Please