我收到一条错误消息,提示 this2.sampleFunction
is not an object ,我已尝试添加一个构造函数以及之前解决方案中的所有内容。
export default class Play extends React.Component {
sampleFunction() {
console.log('Hello');
}
anotherFunction() {
return (
<Button
onPress={() => this.sampleFunction.bind(this)} />
);
}
render() {
<Deck
anotherFunction={this.anotherFunction()}
/>
}
}
编辑:这是甲板组件中的代码,它主要是一个 View 标签
render() {
return (
<View>
{this.props.anotherFunction()}
</View>
);
}
最佳答案
我遇到了这个问题,它和我在评论中描述的一样。您正在将一个元素从 Play 传递到 Deck,但 Deck 需要一个函数。请将 Play 的渲染方法更改为
render() {
return (<Deck
anotherFunction={this.anotherFunction.bind(this)}
/>);
}
关于javascript - 无法在 native react 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48814457/