路由器状态没有与redux一起保留在react-native中

时间:2016-09-16 08:50:43

标签: reactjs react-native redux react-redux react-native-router-flux

我使用reduxreact-nativereact-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>
    )
  }
}

3 个答案:

答案 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)

即使与redux一起使用,

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商店没有持续存在的场景,您还可以传递导航到最后一个场景时使用的道具或设置您想要去的默认场景。我正在努力的应用程序现在已经很好地保持状态。