目前,我使用React Native / Redux使用NavigationStateUtils设置Push和Pop。但是,如果多次按下触发推送操作的按钮,则会收到错误:should not push * route with duplicated key
和*代表route.key
或this.props.navKey
。
可能是错误的原因是什么?我应该如何使用NavigationStateUtils
为每条路线创建一个唯一的密钥?
这是我的设置 - 终极版:
function mapStateToProps(state) {
return {
navigation: state.navReducer,
}
}
export default connect(
mapStateToProps,
{
pushRoute: (route) => push(route),
popRoute: () => pop(),
}
)(NavigationRoot)
我的减速机(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',
}
}
}
编辑控制台日志
编辑2续展
编辑3幻灯片菜单
答案 0 :(得分:4)
像这样调试导航缩减器:
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
中的函数,您可以在reducer中定义自己的修改,但需要确保导航状态具有以下数据格式:
{
// `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
可能是个好主意,因为您可以定义routes,sub-routes ,integrate with react-native-drawer
和integrate with Redux。