我使用redux
和react-native
在react-native-router-flux
中进行了以下redux-persist
配置。我想在刷新时检索最后一个当前路由,但是在重新加载时会覆盖路由堆栈。
这是reducers/index.js
文件
import { combineReducers, createStore, applyMiddleware, compose } from 'redux'
import { persistStore, autoRehydrate } from 'redux-persist'
import { AsyncStorage } from 'react-native'
import logger from 'redux-logger'
import { startServices } from '../services'
import navigation from './navigation'
import devices from './devices'
import rooms from './rooms'
import { ActionConst } from 'react-native-router-flux'
function routes (state = {scene: {}}, action = {}) {
switch (action.type) {
// focus action is dispatched when a new screen comes into focus
case ActionConst.FOCUS:
return {
...state,
scene: action.scene,
};
// ...other actions
default:
return state;
}
}
export const reducers = combineReducers({
routes,
navigation,
devices,
rooms
})
const middleware = [logger()]
const store = createStore(
reducers,
compose(
autoRehydrate(),
applyMiddleware(...middleware)
)
)
persistStore(store, {storage: AsyncStorage}, function onStoreRehydrate () {
startServices()
})
export { store, reducers }
编辑:这是index.js
及其提供者和场景:
import React, { Component } from 'react'
import { store } from './reducers'
import { Provider, connect } from 'react-redux'
import { Router, Scene, Actions } from 'react-native-router-flux';
import Home from './containers/home'
import Login from './containers/login'
import Device from './containers/device'
const scenes = Actions.create(
<Scene key="root">
<Scene key="home" component={Home} />
<Scene key="login" component={Login} />
<Scene key="device" component={Device} />
</Scene>
)
const RouterWithRedux = connect()(Router)
export default class EntryPoint extends Component {
render () {
return (
<Provider store={store}>
<RouterWithRedux scenes={scenes} />
</Provider>
)
}
}
答案 0 :(得分:0)
您需要像这样创建store
:
const store = createStore(
reducers,
compose(
applyMiddleware(...middleware),
autoRehydrate(),
)
);
autoRehydrate
需要成为compose
的一部分。检查this compose
example。
答案 1 :(得分:0)
你能尝试一下吗?我认为发生的事情是createStore的顺序
select * from tableA where orderdate < '%Y-%M-%D 15:00:00.000'
或者其他解决方案应该使用const enhancers = compose(
applyMiddleware(...middleware),
autoRehydrate(),
);
// Create the store with the (reducer, initialState, compose)
const store = createStore(
reducers,
{},
enhancers
);
手动执行,并在reducer中调用它。
答案 2 :(得分:0)
react-native-router-flux
也无法自行恢复场景。 Redux仅跟踪状态,因此如果您希望导航状态保持不变,则必须让您的应用程序在启动时导航到上一个场景。
假设您使用react-native-router-flux
还原,您需要做的就是将应用的初始组件连接到redux以获取状态,然后更改您的场景以匹配。
因此,在为您的应用加载的初始组件场景中,最后执行此类操作以访问您的redux商店:
const mapStateToProps = (state) => {
const { nav } = state
return {
currentScene: nav.scene.name,
}
}
INITIAL_COMPONENT = connect(mapStateToProps)(INITIAL_COMPONENT)
然后在该组件的一个生命周期方法中,您可以像这样重定向:
const { currentScene } = this.props
const initialScene = "Login"
if (currentScene !== initialScene) {
Actions[currentScene]()
}
如果redux商店没有持续存在的场景,您还可以传递导航到最后一个场景时使用的道具或设置您想要去的默认场景。我正在努力的应用程序现在已经很好地保持状态。