【发布时间】:2018-01-10 23:56:38
【问题描述】:
我正在使用 redux-offline 来处理我的应用程序中的离线交互。重新启动应用程序时,我的 redux 商店中的离线状态不会持续存在。
例如,我的应用程序处于离线状态,用户与应用程序交互的方式是调用要调度的操作。 Redux-offline 将 action 转移到 redux store 的离线状态下的 outbox 数组,当应用程序重新上线时应该重试 action。如果我这样做并且有一个动作在离线状态排队,然后重新启动应用程序(仍然离线),redux 商店会保留除离线状态之外的所有内容,因此我丢失了我排队的项目。
我的店铺配置代码如下。我已经以一种方式编写了它,以便在 redux 存储重新水化之前显示启动屏幕。
import React, { Component } from "react"
import { AsyncStorage } from "react-native"
import { Provider } from "react-redux"
import { applyMiddleware, compose, createStore } from "redux"
import thunk from "redux-thunk"
import { autoRehydrate, persistStore } from "redux-persist"
import logger from "redux-logger"
import { offline } from "redux-offline"
import offlineConfig from "redux-offline/lib/defaults"
import reducers from "./redux/modules"
import MyApp from "./MyApp"
import SplashScreen from "./components/home/SplashScreen"
const store = createStore(
reducers,
undefined,
compose(applyMiddleware(thunk, logger), offline(offlineConfig))
)
export default class Setup extends Component {
constructor(props) {
super(props)
this.state = {
rehydrated: false
}
}
componentWillMount() {
persistStore(store, { storage: AsyncStorage }, () => {
this.setState({ rehydrated: true })
})
}
render = () =>
!this.state.rehydrated ? (
<SplashScreen />
) : (
<Provider store={store}>
<MyApp />
</Provider>
)
}
【问题讨论】:
标签: react-native redux