react-native - 如何将 redux-saga 与 redux-persist 或替代品一起使用?
问题描述
我想在应用程序启动时从 API 获取一些数据。在我的应用程序中,我坚持使用 redux,现在我决定使用 redux saga,但它不起作用。不过,我有一个项目的代码实际上至少要花费 20 万美元,所以我重复了该代码。它仍然无法正常工作。我认为,该函数调度不执行。这是我的代码。如果您不知道解决方案,请告诉我替代方案,因为截止日期:)
应用程序.tsx
const App: React.FC = () => {
const {store, persistor} = configureStore();
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={null}>
<Navigator />
</PersistGate>
</Provider>
);
};
配置存储
const persistConfig = {
key: 'root',
storage: AsyncStorage,
};
const persistedReducer = persistReducer(persistConfig, rootReducer);
const sagaMiddleware = createSagaMiddleware();
export default () => {
const store = createStore(
persistedReducer,
compose(
applyMiddleware(sagaMiddleware),
),
);
sagaMiddleware.run(rootSaga);
const persistor = persistStore(store);
return {store, persistor};
};
mapToDispatchProps
const mapDispatchToProps = (dispatch: any) => {
return {
changeWiFiState: (state: boolean) => {
dispatch(changeWiFiState(state));
},
addToScannedCodesNoWifi: (item: string) => {
dispatch(addToScannedCodesNoWifi(item));
},
changeCart: (item: any) => {
dispatch(changeCart(item));
},
saveLocal: (item: any) => {
console.log('fsf')
dispatch({type: "SET_LOCAL_STORAGE_REQUESTED"})
}
};
};
const ScannerScreen = connect(
mapStateToProps,
mapDispatchToProps,
)(_ScannerScreen);
export {ScannerScreen}
根佐贺
export default function* rootSaga() {
takeEvery("SET_LOCAL_STORAGE_REQUESTED", saveLocal)
}
保存本地
function* saga() {
axios.get('https://server-for-rn.herokuapp.com/code/codes').then(response => {
console.log(response.data)
return doArr(response.data)
})
}
export function* saveLocal() {
console.log('fdsfgs')
let res = call(saga)
yield put ({type: "SET_LOCAL_STORAGE", payload: res})
}
解决方案
推荐阅读
- javascript - 根据条件限制复选框
- angular - Angular 6 - promise in lifecycle hooks makes other test fail
- csv - 在 APEX 中使用数据加载定义时如何防止 oracle 数据库更新条目?
- r - Saving user input (Shiny app) to a global variable in R
- php - 使用 MAMP 5.1 (php7.2) 在 Mac 上安装 mcrypt
- javascript - 在 Vue.js 中将值异步推送到数组
- android - 永远不会调用 onReceive() 方法后的应用程序升级
- laravel - The local project works correctly, but it does not find the url's server
- c# - DotNetCore 中的 System.Web.Security MemberShipUser 是否有任何替代方案
- android - 如何编辑图标旁边的通知文本?