我想根据应用程序是否已从错误中恢复来设置条件,以使商店完全持久/重新充水。
在顶级组件(app.js)中,我将导入存储和持久性(从MyStore.js),并将其余应用程序包装在Provider和PersistGate中。该存储在一个单独的文件中,因此可以由某些共享逻辑文件导入和读取。
在加载应用程序组件时,它将检查AsyncStorage以查看其是否已从错误中恢复,并更新状态。我希望该状态确定商店是否持久。
希望有什么建议
// ......... App.js:
componentDidMount = () => { this.checkForPreviousFatalError() }
checkForPreviousFatalError = async () => {
var array = null
try {
array = await AsyncStorage.multiGet(['@lastFatalErrorEpoch', '@previousFatalErrorEpoch'])
} catch(e) {
console.log( "Unable to retrieve date of last 2 errors: " + e );
}
const lastError = array[0][1];
const prevError = array[1][1];
if ( lastError && prevError && parseInt(prevError) + 60000 > parseInt(lastError) ) {
// do not persist store
} else {
// persist store
};
}
render() {
const waitingElement = (
<ImageBackground source={require('./assets/signInBackground.jpg')} style={[ Styles.flexCenter, Styles.flexColumn, { height: "100%" }]}>
<Text style={{ fontSize: 18, marginBottom: 20 }} >{ EnglishNA.RestoringLocalData }</Text>
<Spinner size='large' flexZero={ true } color="white" />
</ImageBackground>
)
return (
<Provider store={ store }>
<PersistGate loading={ waitingElement } persistor={ persistor }>
<LogIn />
</PersistGate>
</Provider>
)
}
}
// ....... MyStore.js
import ReduxThunk from 'redux-thunk';
import { createStore, applyMiddleware } from 'redux';
import ServerCall from './services/ServerCall';
import rootReducer from './reducers';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
const persistConfig = {
key: 'root',
storage: storage,
stateReconciler: autoMergeLevel2 // see "Merge Process" section for details.
};
const pReducer = persistReducer(persistConfig, rootReducer);
export const store = createStore( pReducer, applyMiddleware( ReduxThunk, ServerCall ) );
export const persistor = persistStore(store);
答案 0 :(得分:0)
您应在调用函数checkForPreviousFatalError
之后配置存储。如果有错误,请不要持久存储,也不要在存储中使用persistReducer
。对于加载,您可以使用组件状态。
赞:
const pReducer = persist
? persistReducer(persistConfig, rootReducer)
: rootReducer;
App.js
// ......... App.js:
import configureStore from "./MyStore.js";
//....................
state = {
isLoading: true,
store: null
};
componentDidMount = () => { this.checkForPreviousFatalError() }
checkForPreviousFatalError = async () => {
var array = null
try {
array = await AsyncStorage.multiGet(['@lastFatalErrorEpoch', '@previousFatalErrorEpoch'])
} catch(e) {
console.log( "Unable to retrieve date of last 2 errors: " + e );
}
const lastError = array[0][1];
const prevError = array[1][1];
if ( lastError && prevError && parseInt(prevError) + 60000 > parseInt(lastError) ) {
this.configureStore(false)
// do not persist store
} else {
this.configureStore(true)
// persist store
};
}
configureStore = (persist) => {
configureStore(persist, store => {
this.setState({ isLoading: false, store });
});
}
render() {
if (this.state.isLoading) {
return <ImageBackground source={require('./assets/signInBackground.jpg')} style={[ Styles.flexCenter, Styles.flexColumn, { height: "100%" }]}>
<Text style={{ fontSize: 18, marginBottom: 20 }} >{ EnglishNA.RestoringLocalData }</Text>
<Spinner size='large' flexZero={ true } color="white" />
</ImageBackground>;
}
return (
<View style={{ flex: 1 }}>
<Provider store={this.state.store}>
<AppNavigator />
</Provider>
{this._renderStatusBarIos()}
<MessageBar />
</View>
);
}
MyStore.js文件
import ReduxThunk from "redux-thunk";
import { createStore, applyMiddleware } from "redux";
import ServerCall from "./services/ServerCall";
import rootReducer from "./reducers";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import autoMergeLevel2 from "redux-persist/lib/stateReconciler/autoMergeLevel2";
let store;
export default function configureStore(persist, onComplete: Function) {
const persistConfig = {
key: "root",
storage: storage,
stateReconciler: autoMergeLevel2 // see "Merge Process" section for details.
};
const pReducer = persist
? persistReducer(persistConfig, rootReducer)
: rootReducer;
store = createStore(pReducer, applyMiddleware(ReduxThunk, ServerCall));
persistStore(store, null, () => onComplete(store));
}
如果要导出商店,请在MyStore.js中创建一个函数
export function getStore() {
return store;
}