根据是否已加载Persist Gate设置状态

时间:2019-04-01 05:07:36

标签: react-native

在我的App.js中,我要在Persist Gate完成加载后将布尔变量的状态设置为true。我正在使用自定义的启动画面组件,因此我想在<PersistGate>完成加载后将布尔变量的状态设置为true,请问我该怎么做

Store- Index.js

    import { createStore } from "redux";
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' 
import rootReducer from "../reducers/index";
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
const persistConfig = {
    key: 'root',
    storage,
    stateReconciler: autoMergeLevel2
  }
const persistedReducer = persistReducer(persistConfig, rootReducer)
export default () => {
  let store = createStore(persistedReducer)
  let persistor = persistStore(store)
  return { store, persistor }
}

App.js

import {
    Platform,
    StyleSheet,
    Text,
    Image,
    Dimensions,
    StatusBar,
    ScrollView,
    View,
    DeviceEventEmitter,
    FlatList,
    ActivityIndicator,
    AsyncStorage
} from 'react-native';
import Splash from './components/Splash';
import Home from './components/Home';
import MusicFiles from 'react-native-get-music-files';
import Permissions from 'react-native-permissions';
import { PersistGate } from 'redux-persist/integration/react';


export default class App extends Component{
 constructor(props) {
        super(props);
        this.state = {
            timePassed: false,
            photoPermission: '',
            songs: [], 
            loaded: true,
            loading: false
        };
    }
  render() {
    if (!this.state.loaded) {
        return (
            <Splash/>
           );
    } else {
        return (
            <View style={styles.linearGradient}>
            <Provider store={store}>
            <PersistGate loading={null} persistor={persistor}>
            <Home songs={this.state.songs}/>
            </PersistGate>
            </Provider>
            </View>
        );

    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以将启动组件放到加载道具中,也可以使用onBeforeLift

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

 return (
        <View style={styles.linearGradient}>
          <Provider store={store}>
            <PersistGate 
              loading={<Splash/>}
              onBeforeLift={this.onBeforeLift}
              persistor={persistor}>
               <Home songs={this.state.songs}/>
          </PersistGate>
         </Provider>
       </View>
    );