目前,我已经使用React Native/Redux使用NavigationStateUtils设置了Push和Pop。但是,当多次按下触发“推”操作的按钮时,出现错误:should not push * route with duplicated key和*表示route.keythis.props.navKey

错误原因可能是什么?我应该如何使用NavigationStateUtils为每个单独的路线创建唯一的 key ?

这是我的设置-
Redux:

function mapStateToProps(state) {
  return {
    navigation: state.navReducer,
  }
}

export default connect(
  mapStateToProps,
  {
    pushRoute: (route) => push(route),
    popRoute: () => pop(),
  }
)(NavigationRoot)

我的reducer(navReducer.js):
const initialState = {
  index: 0,
  key: 'root',
  routes: [{
   key: 'login',
   title: 'Login',
   component: Login,
   direction: 'horizontal',
  }]
}

function navigationState (state = initialState, action) {
  switch(action.type) {
    case PUSH_ROUTE:
      if (state.routes[state.index].key === (action.route && action.route.key)) return state
    return NavigationStateUtils.push(state, action.route)

    case POP_ROUTE:
      if (state.index === 0 || state.routes.length === 1) return state
      return NavigationStateUtils.pop(state)

   default:
     return state

  }
}

export default navigationState

这些方法可以处理推和弹出以及如何设置导航栏后退(弹出)按钮:
  _renderScene (props) {
    const { route } = props.scene

    return (
      <route.component _handleNavigate={this._handleNavigate.bind(this)} {...route.passProps} actions={this.props}/>
    )
  }

  _handleBackAction() {
    if (this.props.navigation.index === 0) {
      return false
    }
    this.props.popRoute()
    return true
  }

  _handleNavigate(action) {
    switch (action && action.type) {
      case 'push':
        this.props.pushRoute(action.route)
        return true
      case 'back':
      case 'pop':
        return this._handleBackAction()
      default:
        return false
    }
  }

renderOverlay = (sceneProps) => {
if(0 < sceneProps.scene.index)
{
  return (
    <NavigationHeader
      {...sceneProps}
      renderLeftComponent={() => {
        switch(sceneProps.scene.route.title){
          case 'Home':
            return (
              <TouchableHighlight onPress={() => this._handleBackAction()}>
                <Text}>X</Text>
              </TouchableHighlight>
            )
          }
        }
      }
    />
  )
}
  }

  render() {
    return (
        <NavigationCardStack
          direction={this.props.navigation.routes[this.props.navigation.index].direction}
          navigationState={this.props.navigation}
          onNavigate={this._handleNavigate.bind(this)}
          renderScene={this._renderScene}
          renderOverlay={this.renderOverlay}
        />
    )
  }

并由类似这样的组件调用:
const route = {
  home: {
    type: 'push',
    route: {
      key: 'home',
      title: 'Home',
      component: Home,
      direction: 'vertical',
    }
  }
}

编辑控制台日志

javascript - 如何在React Native/Redux中使用推送路由为NavigationStateUtils创建唯一键?-LMLPHP
javascript - 如何在React Native/Redux中使用推送路由为NavigationStateUtils创建唯一键?-LMLPHP

编辑2续

javascript - 如何在React Native/Redux中使用推送路由为NavigationStateUtils创建唯一键?-LMLPHP

编辑3幻灯片菜单

javascript - 如何在React Native/Redux中使用推送路由为NavigationStateUtils创建唯一键?-LMLPHP

最佳答案

像这样调试您的导航 reducer :

function navigationState (state = initialState, action) {
  switch(action.type) {
    case PUSH_ROUTE:
      console.log('action', action);
      if (state.routes[state.index].key === (action.route && action.route.key)) return state
      const newNavigationState = NavigationStateUtils.push(state, action.route);
      console.log('newNavigationState', newNavigationState);
      return newNavigationState;

    case POP_ROUTE:
      if (state.index === 0 || state.routes.length === 1) return state
      return NavigationStateUtils.pop(state)

   default:
     return state

  }
}

编辑

由于NavigationStateUtils.push的工作方式(请参阅here),它需要一条全新的路由才能推送到路由堆栈。根据您的导航流程,您不能再次使用它回家,您必须使用 NavigationStateUtils.pop NavigationStateUtils.reset 等其他函数以及其他函数(浏览该文件)。但是,您不仅限于NavigationStateUtils中的函数,还可以在化简器中定义自己的修改,但是您需要确保导航状态具有以下数据格式:
{
  // `routes` needs to be an array of objects and each object
  // needs to have a unique `key` property
  routes: [{
    key: 'anything', // must be unique in this `routes` array
    ...anyOtherData
  }],
  // `index` is required, and has to be a number that refers
  // to the index of the route in the routes array that is active
  index: 1,
}

编辑

根据您在注释中的要求,每当您碰到一个抽屉项目时,都需要reset以该路线为起点的导航路线堆栈,然后就可以开始推送和弹出了。
function navigationState (state = initialState, action) {
  switch(action.type) {
    case PUSH_ROUTE:
      if (state.routes[state.index].key === (action.route && action.route.key)) return state
      return NavigationStateUtils.push(state, action.route)

    case POP_ROUTE:
      if (state.index === 0 || state.routes.length === 1) return state
      return NavigationStateUtils.pop(state)

    case DRAWER_ITEM_PRESS:
      // `action.route` is simply the route object of the drawer item you pressed
      return NavigationStateUtils.reset(state, [action.route], 0);
    default:
      return state;
  }
}

另外,我看到根据您的要求,尝试使用 react-native-router-flux 可能是一个好主意,因为您将能够定义routessub-routesintegrate with react-native-drawer integrate with Redux

09-25 19:05