Javascript 如何在React Native+;中使用NavigationStateUtils实现Navigator.replace;雷杜?
在myJavascript 如何在React Native+;中使用NavigationStateUtils实现Navigator.replace;雷杜?,javascript,html,reactjs,react-native,react-jsx,Javascript,Html,Reactjs,React Native,React Jsx,在mynavReducer.js中,具有以下处理.pop和.push的功能。但是遵循相同的指导原则,使用导航stateutils,我如何实现.replace在何处用新路线替换当前场景?很像导航器。替换 import { NavigationExperimental } from 'react-native' const { StateUtils: NavigationStateUtils } = NavigationExperimental function navigationStat
navReducer.js
中,具有以下处理.pop和.push的功能。但是遵循相同的指导原则,使用导航stateutils
,我如何实现.replace在何处用新路线替换当前场景?很像导航器。替换
import { NavigationExperimental } from 'react-native'
const {
StateUtils: NavigationStateUtils
} = NavigationExperimental
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
但它已经在NavigationStateUtils上了
/**
* Replace a route by a key.
* Note that this moves the index to the positon to where the new route in the
* stack is at.
*/
function replaceAt(
state: NavigationState,
key: string,
route: NavigationRoute,
): NavigationState {
const index = indexOf(state, key);
return replaceAtIndex(state, index, route);
}
/**
* Replace a route by a index.
* Note that this moves the index to the positon to where the new route in the
* stack is at.
*/
function replaceAtIndex(
state: NavigationState,
index: number,
route: NavigationRoute,
): NavigationState {
invariant(
!!state.routes[index],
'invalid index %s for replacing route %s',
index,
route.key,
);
if (state.routes[index] === route) {
return state;
}
const routes = state.routes.slice();
routes[index] = route;
return {
...state,
index,
routes,
};
}
查看“navigationStateUtils.jumpTo”方法:
它通过给定的路由密钥重定向到路由重要注意事项:此操作后,您必须注意通过“navigationStateUtils.push”再次按下相同的路线键
下面是方法的内部结构:
jumpTo(state: NavigationState, key: string): NavigationState {
const index = NavigationStateUtils.indexOf(state, key);
return NavigationStateUtils.jumpToIndex(state, index);
}
使用实现方法编辑的代码版本:
import { NavigationExperimental } from 'react-native'
const {
StateUtils: NavigationStateUtils
} = NavigationExperimental
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 JUMP_TO_ROUTE:
return NavigationStateUtils.jumpTo(action.routeKey)
default:
return state
}
}
export default navigationState
我也对你感兴趣answer@Fantasim运气好吗?对不起,我不是很理解。在我接受答案并投票之前,我将如何整合到我的navReducer中?您能否展示一个修改后的示例?我正在尝试使用与设置PUSH_ROUTE
和POP_ROUTE
相同的格式。我只是设置了一个示例项目,主要是关于导航实验: