在应用程序启动时重置 reducer 状态,同时使用 redux-persist 保持状态
reset reducer state when application start while have persist state using redux-persist
我正在使用 redux-persist 和异步存储在我的 react-native-application 中持久化数据。
这是我的 redux 商店的配置
import AsyncStorage from '@react-native-community/async-storage';
import {applyMiddleware, createStore} from 'redux';
import {composeWithDevTools} from 'redux-devtools-extension';
import {persistReducer, persistStore} from 'redux-persist';
import createSagaMiddleware from 'redux-saga';
import rootReducer from '../reducers';
import rootSaga from '../sagas';
const sagaMiddleware = createSagaMiddleware();
// Middleware: Redux Persist Config
const persistConfig = {
// Root
key: 'root',
// Storage Method (React Native)
storage: AsyncStorage,
// Whitelist (Save Specific Reducers)
whitelist: ['authReducer',],
// Blacklist (Don't Save Specific Reducers)
blacklist: [''],
};
// Middleware: Redux Persist Persisted Reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);
// Redux: Store
const store = createStore(
persistedReducer,
composeWithDevTools(applyMiddleware(sagaMiddleware)),
);
sagaMiddleware.run(rootSaga);
// Middleware: Redux Persist Persister
let persistor = persistStore(store);
// Exports
export {store, persistor};
当我向服务器发出请求时,我使用三种类型的动作调度,例如
LOGIN_REQUEST
LOGIN_SUCCESS
LOGIN_FAILURE
并根据请求调度操作 LOGIN_REQUEST(将加载状态设置为 true 以加载动画并禁用登录按钮),请求成功后 LOGIN_SUCCESS action is dispatch and LOGIN_FAILURE 当请求失败时(并将加载状态设置为false)
这是减速器
...
case LOGIN_SUCCESS: {
return {
...state,
user: action.payload,
loading : false
};
}
case LOGIN_REQUEST: {
return {
...state,
loading: true,
};
}
case LOGIN_FAILURE: {
return {
...state,
loading: false,
};
}
...
加载动画效果很好。
如果在请求正在进行时应用程序关闭,并且加载状态仍然为真,因为没有调度 LOGIN_FAILURE 或 LOGIN_SUCCESS 操作,问题就会出现,
下次打开应用程序时,加载状态为 true 并出现加载动画
我研究了一些解决方案,例如启动时自动调度操作的更新状态 persist/REHYDRATE like
...
case "persist/REHYDRATE": {
return {
...state,
loading : false
};
}
...
或
黑名单状态喜欢reducerName.loading喜欢
...
storage: AsyncStorage,
// Whitelist (Save Specific Reducers)
whitelist: ['authReducer',],
// Blacklist (Don't Save Specific Reducers)
blacklist: ['authReducer.loading'],
...
有没有更好的方法来解决这种情况
谢谢
您可以使用 AppState
检查并禁用加载。
import {AppState} from 'react-native';
useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);
return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, []);
const _handleAppStateChange = (nextAppState) => {
if (appState.current === 'active' && nextAppState.match(/inactive|background/)) {
//check if loading is true, set is to false if it is
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
};
我正在使用 redux-persist 和异步存储在我的 react-native-application 中持久化数据。
这是我的 redux 商店的配置
import AsyncStorage from '@react-native-community/async-storage';
import {applyMiddleware, createStore} from 'redux';
import {composeWithDevTools} from 'redux-devtools-extension';
import {persistReducer, persistStore} from 'redux-persist';
import createSagaMiddleware from 'redux-saga';
import rootReducer from '../reducers';
import rootSaga from '../sagas';
const sagaMiddleware = createSagaMiddleware();
// Middleware: Redux Persist Config
const persistConfig = {
// Root
key: 'root',
// Storage Method (React Native)
storage: AsyncStorage,
// Whitelist (Save Specific Reducers)
whitelist: ['authReducer',],
// Blacklist (Don't Save Specific Reducers)
blacklist: [''],
};
// Middleware: Redux Persist Persisted Reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);
// Redux: Store
const store = createStore(
persistedReducer,
composeWithDevTools(applyMiddleware(sagaMiddleware)),
);
sagaMiddleware.run(rootSaga);
// Middleware: Redux Persist Persister
let persistor = persistStore(store);
// Exports
export {store, persistor};
当我向服务器发出请求时,我使用三种类型的动作调度,例如
LOGIN_REQUEST
LOGIN_SUCCESS
LOGIN_FAILURE
并根据请求调度操作 LOGIN_REQUEST(将加载状态设置为 true 以加载动画并禁用登录按钮),请求成功后 LOGIN_SUCCESS action is dispatch and LOGIN_FAILURE 当请求失败时(并将加载状态设置为false) 这是减速器
...
case LOGIN_SUCCESS: {
return {
...state,
user: action.payload,
loading : false
};
}
case LOGIN_REQUEST: {
return {
...state,
loading: true,
};
}
case LOGIN_FAILURE: {
return {
...state,
loading: false,
};
}
...
加载动画效果很好。
如果在请求正在进行时应用程序关闭,并且加载状态仍然为真,因为没有调度 LOGIN_FAILURE 或 LOGIN_SUCCESS 操作,问题就会出现,
下次打开应用程序时,加载状态为 true 并出现加载动画
我研究了一些解决方案,例如启动时自动调度操作的更新状态 persist/REHYDRATE like
...
case "persist/REHYDRATE": {
return {
...state,
loading : false
};
}
...
或
黑名单状态喜欢reducerName.loading喜欢
...
storage: AsyncStorage,
// Whitelist (Save Specific Reducers)
whitelist: ['authReducer',],
// Blacklist (Don't Save Specific Reducers)
blacklist: ['authReducer.loading'],
...
有没有更好的方法来解决这种情况 谢谢
您可以使用 AppState
检查并禁用加载。
import {AppState} from 'react-native';
useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);
return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, []);
const _handleAppStateChange = (nextAppState) => {
if (appState.current === 'active' && nextAppState.match(/inactive|background/)) {
//check if loading is true, set is to false if it is
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
};