PersistGate輕松幾步讓Redux實現數據持久化


在開發的過程中,數據用redux管理,覺得希望將數據持久化保存,也就是說當用戶下一次打開app或網站的時候,我們希望瀏覽器/APP自動加載出上次的數據,怎么辦?有沒有一個🙆‍♂統一的方式?
有的,這就是簡單易用的redux-persist,事情比想象的簡單太多了。
話不多說,上代碼!官方示例代碼:

import { PersistGate } from 'redux-persist/es/integration/react'

import configureStore from './store/configureStore'

const { persistor, store } = configureStore()

const onBeforeLift = () => {
  // take some action before the gate lifts
}

export default () => (
  <Provider store={store}>
    <PersistGate 
      loading={<Loading />}
      onBeforeLift={onBeforeLift}
      persistor={persistor}>
      <App />
    </PersistGate>
  </Provider>
)

首先你的結構應該是擁有reducers的,在我的代碼中,我還加入了redux-thunk中間件,如果你不懂,可以忽略這個。

 import React, { Component } from 'react';      
 import {AppRegistry} from 'react-native';      
 import { createStore ,applyMiddleware } from 'redux'
 import { Provider } from 'react-redux'     
 import App from "./src/App";       
 import reducers from './src/reducers/reducers' 
 import thunk from 'redux-thunk';       
 export default class CoinOnline extends Component {        
   render() {
     const store = createStore(App,applyMiddleware(thunk)); 
     return (
       <Provider store={store}>
        <App />
       </Provider>      
       );
     }
 }
AppRegistry.registerComponent('CoinOnline', () => CoinOnline); 

我是怎么應用的?

第一步,引入我們需要的方法

import {persistStore, persistCombineReducers} from 'redux-persist';
import { PersistGate } from 'redux-persist/es/integration/react';

第二步,重新封裝reducer,用persistCombineReducers()方法加載配置和reducer

const config = {
    key: 'root',
    storage,
};
let reducer = persistCombineReducers(config, reducers);

第三步,在redux的 內層嵌套

<Provider store={store}>
                <PersistGate persistor={persistor}>
                    <App />
                </PersistGate>
</Provider>

輕松三步,簡單吧!話不多說直接上代碼

import React, { Component } from 'react';
import {
    AppRegistry,
} from 'react-native';
import { createStore ,applyMiddleware, compose } from 'redux'
import { Provider } from 'react-redux'
import App from "./src/App";
import reducers from './src/reducers/reducers'
import thunk from 'redux-thunk';
import {persistStore, persistCombineReducers} from 'redux-persist';
import { PersistGate } from 'redux-persist/es/integration/react';

import storage from 'redux-persist/es/storage' // default: localStorage if web, AsyncStorage if react-native

const config = {
    key: 'root',
    storage,
};

function configureStore(){
    let reducer = persistCombineReducers(config, reducers);
    let store = createStore(reducer, applyMiddleware(thunk));
    let persistor = persistStore(store);
    return { persistor, store }
}
export default class CoinOnline extends Component {
    render() {
        const { persistor, store } = configureStore();
        return (
            <Provider store={store}>
                <PersistGate persistor={persistor}>
                    <App />
                </PersistGate>
            </Provider>

        );
    }
}
AppRegistry.registerComponent('CoinOnline', () => CoinOnline);

多說一句,如果createStore有需要加載多個參數,需要用compose將其拼裝起來。

比如在測試時我還使用了remote-redux-devtools調試神器,代碼如下

let store = createStore(reducer, compose(applyMiddleware(thunk),devToolsEnhancer({ realtime: true, port: 8000 })));

下次,每次啟動之前redux-persit都會默認先dispatch兩個動作

PERSIT和REHYDRATE,會把上一次的redux中的states注入到當前組件中,即完成了持久化。
學習自:https://www.jianshu.com/p/a7a10ac59511


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM