Using redux-persist to localize redux to store data, is there really a storage redux, in localstorage, but the value is initialized?

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

Mar.04,2021

redux-persist this thing, look at the document on npmjs, there is a State Reconciler thing, there are three ways to see this thing: hardSet,autoMergeLevel1,autoMergeLevel2.

const persistConfig = {
  key: 'root',
  storage,
  stateReconciler: autoMergeLevel1,
}

can be updated automatically if you put it into the configuration? But. Eggs are useless! Eggs are useless! Eggs are useless! Eggs are useless!

then I thought, since he doesn't update automatically, I'll just add him to localstorage manually. It's a success. The code is as follows
under your reducers

.
export default function reducer(state = initState, action) {
    switch (action.type) {

        case SAVEUSER:
            state.user_data = action.UserData;

            var locStore = JSON.parse(localStorage.getItem('persist:root'));
            locStore.webState = JSON.stringify(state);

            localStorage.setItem('persist:root',JSON.stringify(locStore));
            
            return state;
        case SAVEACTID:
            state.ActID = action.ActID;

            console.log(state,'actidddddddddddddddd');
            
            var locStore = JSON.parse(localStorage.getItem('persist:root'));
            locStore.webState = JSON.stringify(state);
            localStorage.setItem('persist:root',JSON.stringify(locStore));

            return state;
        default:
            return state
    }
}

the key statement is the operation of localstorage. Originally, I thought that it could be done without the unsafe and unreliable user-modifiable way of locakstorage, but now I still use localstorage!


be careful with localstorage and other h5 storage, let alone abuse ~

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3750e-e5d3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3750e-e5d3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?