Reactjs 无法从React Native引用导航器
我正在处理一个React原生项目,不知道为什么不能引用Navigator对象。我收到一个错误:Reactjs 无法从React Native引用导航器,reactjs,react-native,Reactjs,React Native,我正在处理一个React原生项目,不知道为什么不能引用Navigator对象。我收到一个错误:undefined不是一个对象(评估_this2.refs.\u navigator.push)。一秒钟后,当mysetTimeout函数触发时,会发生错误 谢谢你的帮助 这是我的密码 App.js // ... some code initialization... class App extends Component { constructor(props) { super(prop
undefined不是一个对象(评估_this2.refs.\u navigator.push)
。一秒钟后,当mysetTimeout
函数触发时,会发生错误
谢谢你的帮助
这是我的密码
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}
];
/。。。一些代码初始化。。。
类应用程序扩展组件{
建造师(道具){
超级(道具);
}
componentDidMount(){
设置超时(()=>{
此.refs._navigator.push(路由[1]);
}, 1000);
}
render(){
返回(
{
交换机(路由索引){
案例0:
返回;
案例1:
返回;
违约:
返回;
}
}}
ref={(导航)=>{this.\u navigator=nav;}}
/>
);
}
}
常数路由=[
{title:'Home',索引:0},
{标题:'About',索引:1},
{标题:“汽车”,索引:2}
];
尝试在componentDidMount中执行此操作:
componentDidMount() {
setTimeout(() => {
this._navigator.push(routes[1]);
}, 1000);
}
这是因为您引用的导航是这样的。_navigatorref.Hey@Nader,非常感谢。这对我来说似乎有点困惑,因为在纯React中,我必须使用“refs”这个词才能得到这个结果。顺便说一句谢谢。@PabloDarde不是真的,您正在分配
this.\u navigator
在ref回调中,因此它在那里可用。哦,您是对的,也许“refs”关键字对于引用元素的不推荐方式是必需的,例如:不推荐this。如果您这样做
,refs
将有意义。然后您可以执行this.refs.nav