React native 正在从内部NavigatorIOS.OnRightButton访问this.props.navigator按
我正在处理NavigatorIOS组件上的“onRightButtonPress”事件。处理程序似乎没有得到this.props.navigator的引用React native 正在从内部NavigatorIOS.OnRightButton访问this.props.navigator按,react-native,React Native,我正在处理NavigatorIOS组件上的“onRightButtonPress”事件。处理程序似乎没有得到this.props.navigator的引用 var CBSupport = React.createClass({ _handleNextButtonPress: function() { debugger AlertIOS.alert("Be A Lert"); // this.props does not contain a 'navigator'
var CBSupport = React.createClass({
_handleNextButtonPress: function() {
debugger
AlertIOS.alert("Be A Lert");
// this.props does not contain a 'navigator' property
this.props.navigator.push({
component: Login,
title: 'Login'
});
},
render: function() {
return(
<NavigatorIOS
style={styles.container}
initialRoute={{
component: Accounts,
title: 'Accounts',
rightButtonTitle: 'Add',
onRightButtonPress: this._handleNextButtonPress,
}}
/>
)
}
})
var CBSupport=React.createClass({
_handleNextButtonPress:function(){
调试器
AlertIOS.alert(“成为一个Lert”);
//this.props不包含“navigator”属性
这个是.props.navigator.push({
组件:登录,
标题:“登录”
});
},
render:function(){
返回(
)
}
})
我可能做错了什么,但我正试图通过点击NavigatorIOS组件上的右键导航到一个新场景
我错过了什么
非常感谢您的帮助,
伊约纳斯
更新
在科林·拉姆齐(Colin Ramsay)提出以下使用REF的建议后,我得到了以下解决方案:
var CBSupport = React.createClass({
_handleNextButtonPress: function() {
this.refs.nav.push({
component: Login,
title: 'Login'
});
},
render: function() {
return(
<NavigatorIOS
ref="nav"
style={styles.container}
initialRoute={{
component: Accounts,
title: 'Accounts',
rightButtonTitle: 'Add',
onRightButtonPress: this._handleNextButtonPress,
}}
/>
)
}
})
var CBSupport=React.createClass({
_handleNextButtonPress:function(){
this.refs.nav.push({
组件:登录,
标题:“登录”
});
},
render:function(){
返回(
)
}
})
非常感谢Colin。在文档中说: 它[navigator]作为道具传递给由渲染的任何组件 导航器 我认为这是指NavigatorIOS的任何子级,而在您的示例中,实际上您将NavigatorIOS作为CBSupport组件的子级。但是,另一种方法是:
var CBSupport = React.createClass({
_handleNextButtonPress: function() {
// Get by ref not prop
this.refs.nav.push({
component: Login,
title: 'Login'
});
},
render: function() {
return(
<NavigatorIOS
style={styles.container},
ref: 'nav', // added this
initialRoute={{
component: Accounts,
title: 'Accounts',
rightButtonTitle: 'Add',
onRightButtonPress: this._handleNextButtonPress,
}}
/>
)
}
})
var CBSupport=React.createClass({
_handleNextButtonPress:function(){
//通过ref而不是道具
this.refs.nav.push({
组件:登录,
标题:“登录”
});
},
render:function(){
返回(
)
}
})
请注意,我在NavigatorIOS中添加了一个值为“nav”的ref,我们可以使用它在事件处理程序中获得它。由于NavigatorIOS上也自动提供了所有Navigator方法,因此您可以根据需要调用
push
。感谢Colin的快速回复。有一个小错误,我不得不将'ref'从initialRoute内部移动到NavigatorIOS级别。哎呀,这就是我的意思!对不起,我已经编辑了答案。