• react redux数据持久化


    首先安装:npm i  redux-persist -S

    安装以后直接进入src/index.js 写入以下代码   直接拿来用

    import React,{Component} from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from "react-redux"
    import { createStore,applyMiddleware } from "redux"
    import thunk from "redux-thunk"

    import './index.css';
    import App from './App';
    import * as serviceWorker from './serviceWorker';

    import rootReducer from "./reducers"
     
    import {persistStore,persistReducer} from "redux-persist"
    import {PersistGate} from "redux-persist/es/integration/react"
    import storage from 'redux-persist/es/storage'
    const config = {
        key:'root',
        storage,
    }

    function configureStore(){
        let reducer = persistReducer(config,rootReducer);
        let store = createStore(reducer,applyMiddleware(thunk));
        let persistor = persistStore(store)
        
        return {persistor,store}
    }

    export default class Hello extends Component{
        render(){
            
            const {persistor,store} = configureStore();
            console.log(111)
            return(
                <Provider store={store}>
                    <PersistGate persistor={persistor}>
                        <div>
                            <App />
                        </div>
                    </PersistGate>
                    
                </Provider>
            )
        }
    }
     
    ReactDOM.render(<Hello />, document.getElementById('root'));

    serviceWorker.unregister();

      

  • 相关阅读:
    CCF-CSP-201803-3 URL映射
    CCF-CSP-201803-2 碰撞的小球
    CCF-CSP-201803-1 跳一跳
    SSH协议基础学习
    关于Kali-linux2019.4系统安装后乱码问题
    git版本控制工具学习--修改分支名字
    Git版本控制工具操作学习系列-克隆项目
    python函数int()转型报错
    Centos关机与重启命令收集学习
    Git commit 提交规范 & 规范校验
  • 原文地址:https://www.cnblogs.com/zqxi/p/12462166.html
Copyright © 2020-2023  润新知