我正在研究一个React Native项目,我不知道为什么我不能引用Navigator对象。我收到一个错误:undefined is not an object (evaluating _this2.refs._navigator.push)
。一秒钟后,当我的setTimeout
函数触发时,就会发生错误。
感谢任何帮助
这是我的代码。
App.js
// ... some code initialization...
class App extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
setTimeout(() => {
this.refs._navigator.push(routes[1]);
}, 1000);
}
render() {
return(
<Navigator
initialRoute={routes[0]}
initialRouteStack={routes}
renderScene={(route, navigator) => {
switch (route.index) {
case 0:
return <Home />;
case 1:
return <About />;
default:
return <Home />;
}
}}
ref={(nav) => { this._navigator = nav; }}
/>
);
}
}
const routes = [
{title: 'Home', index: 0},
{title: 'About', index: 1},
{title: 'Cars', index: 2}
];
答案 0 :(得分:0)
尝试在componentDidMount中执行此操作:
componentDidMount() {
setTimeout(() => {
this._navigator.push(routes[1]);
}, 1000);
}
这是因为您在Navigator ref。
中将nav引用为this._navigator