React native 反应导航无法读取属性';路线';未定义的
我的应用程序使用导航路由器,按照API方法,出现以下问题: 反应导航:为什么“路线”未定义React native 反应导航无法读取属性';路线';未定义的,react-native,routes,react-navigation,React Native,Routes,React Navigation,我的应用程序使用导航路由器,按照API方法,出现以下问题: 反应导航:为什么“路线”未定义 const simpleap=TabNavigator({}) const Simple=StackNavigator({ 主页:{screen:SimpleApp}, 登录:{屏幕:登录}, }) const defaultGetStateForAction=Simple.router.getStateForAction; Simple.router.getStateForAction=(操作,状态)=
const simpleap=TabNavigator({})
const Simple=StackNavigator({
主页:{screen:SimpleApp},
登录:{屏幕:登录},
})
const defaultGetStateForAction=Simple.router.getStateForAction;
Simple.router.getStateForAction=(操作,状态)=>{
log('action==',action);
log('state==',state);
console.log(“……getStateForAction……”);
如果(真){
常数路由=[
…州路线,
{key:'A',routeName:'Login',params:{name:action.name1},
];
返回{
……国家,
路线,
索引:routes.length-1,
};
}
返回defaultGetStateForAction(操作,状态);
};
导出默认类应用程序扩展组件{
render(){
返回(
)
}
}
TypeError:无法读取未定义的属性“routes”
我们基本上覆盖了getStateForAction函数。
此功能存在于StackRouter文件中。它所做的是检查初始状态是否未定义。如果是,它就会创造它
StackRouter.js
getStateForAction(
action: NavigationAction,
state: ?NavigationState
): ?NavigationState {
// Set up the initial state if needed
if (!state) {
let route = {};
... // The code present in the file.
}
要了解更多信息,请查看StackRouter.js
由于未设置初始状态,因此状态本身未定义,…代码中的state.routes产生错误
目前,这将起作用:
Simple.router.getStateForAction = (action, state) => {
console.log('action ===',action);
console.log('state ===',state);
console.log('......getStateForAction........');
// change the condition to check if state exists.
if (state) {
const routes = [
...state.routes,
{key: 'A', routeName: 'Login', params: { name: action.name1 }},
];
return {
...state,
routes,
index: routes.length - 1,
};
}
return defaultGetStateForAction(action, state);
};
Simple.router.getStateForAction = (action, state) => {
console.log('action ===',action);
console.log('state ===',state);
console.log('......getStateForAction........');
// change the condition to check if state exists.
if (state) {
const routes = [
...state.routes,
{key: 'A', routeName: 'Login', params: { name: action.name1 }},
];
return {
...state,
routes,
index: routes.length - 1,
};
}
return defaultGetStateForAction(action, state);
};