Javascript 未定义react native this.refs.nav
新到react native,尝试使用导航上的按钮呈现主页以转到用户页Javascript 未定义react native this.refs.nav,javascript,facebook,react-native,Javascript,Facebook,React Native,新到react native,尝试使用导航上的按钮呈现主页以转到用户页 var TourChampIOs = React.createClass({ getInitialState() { .... }, componentWillMount() { .... }, _handleUserDataPress: function() { // Get by ref not prop th
var TourChampIOs = React.createClass({
getInitialState() {
....
},
componentWillMount() {
....
},
_handleUserDataPress: function() {
// Get by ref not prop
this.refs.nav.push({
component: UserPage,
title: 'User Page'
});
},
render: function() {
return <NavigatorIOS
style={styles.container}
initialRoute={{
title: 'Tour Champ',
component: ThemeList,
rightButtonTitle: tc.user.displayName.split(' ')[0],
onRightButtonPress: this._handleUserDataPress
}}/>;
}
var TourChampIOs=React.createClass({
getInitialState(){
....
},
组件willmount(){
....
},
_handleUserDataPress:函数(){
//通过ref而不是道具
this.refs.nav.push({
组件:UserPage,
标题:“用户页面”
});
},
render:function(){
返回;
}
我得到的错误是
错误:无法读取未定义的属性“push”
当从渲染返回时,需要为组件指定ref属性,并将“nav”作为其值
render: function() {
return <NavigatorIOS
ref="nav" // Here is the change
style={styles.container}
initialRoute={{
title: 'Tour Champ',
component: ThemeList,
rightButtonTitle: tc.user.displayName.split(' ')[0],
onRightButtonPress: this._handleUserDataPress
}}/>;
}
render:function(){
返回;
}
我不确定您想做什么,也不确定ref的使用是否真的适合您的需要。我当然不会使用ref来储存您想点击的页面的引用
如果您仍然选择执行这种丑陋的操作,只需在渲染函数中添加ref:
点击这个
在handleClick函数中检索ref
handleClick = function() {
doSomethingWithThisref(this.refs.UserPage);
// or
// set a new state for this component
this.setState({someState:"somedata", function() {
React.findDOMNode(this.refs.UserPage);
});
}
ref用于引用DOM,而不是React render函数返回的虚拟DOM。
下面是一个很好的例子:
顺便说一下,您无法访问this.refs,因为您可能没有在渲染函数中设置任何ref。但是,在我看来,您不应该使用处理程序动态设置refs,而应该在渲染函数中设置refs
希望它能帮助你,不要跟着你。你能粘贴示例代码吗?你真的不应该在评论中发布代码,为什么不改为编辑你的答案呢?似乎@François的答案取代了我的答案-看看这个:我想知道为什么我投了两张反对票。我写了什么愚蠢的东西吗?有人能向我解释为什么我投了反对票吗?是不是有什么愚蠢的事吗?嗯,也许那些投反对票的人你只看到了丑陋的东西,没有看到你说那是不推荐的。啊,顺便说一句,如果你不想要反对票的记录,你可以简单地删除你的答案……我上次收到反对票时就这么做了。。