【发布时间】:2017-08-18 11:20:24
【问题描述】:
所需的行为是将参数(文本)传递给 onClick 处理程序以对其进行控制台记录,但似乎我的语法有问题。
如果我将论点如下所示,它可以正常工作:
export default class Nav extends Component {
componentDidMount() {
this.props.pickNumber(3);
}
onPress() {
console.log('FOOOBAAR');
}
render() {
return (
<View>
<Text>####################</Text>
<Text>Intro Screen</Text>
<Text>Number: {this.props.numbers}</Text>
<TouchableHighlight onPress={this.onPress.bind(this)}>
<Text>Go to Foo</Text>
</TouchableHighlight>
</View>
);
}
}
但是,如果我想将参数传递给 onPress 处理程序,它会抱怨“无法读取未定义的属性 'bind'。
export default class Nav extends Component {
componentDidMount() {
this.props.pickNumber(3);
}
onPress(txt) {
console.log(txt);
}
render() {
return (
<View>
<Text>####################</Text>
<Text>Intro Screen</Text>
<Text>Number: {this.props.numbers}</Text>
<TouchableHighlight onPress={this.onPress('foo').bind(this)}>
<Text>Go to Foo</Text>
</TouchableHighlight>
</View>
);
}
}
谢谢
补充: 如果我将其更改为:
onPress={this.onPress.bind('foo')}
它也不起作用。
【问题讨论】:
-
顺便说一句,从技术上讲,
bind在这种情况下根本不需要,因为this没有在onPress函数中引用。
标签: binding react-native