【发布时间】:2019-07-13 21:02:26
【问题描述】:
我正在尝试将方法从我的父组件传递给子组件。我认为我的代码是正确的,但它仍然显示错误 undefined is not an object(evalating '_this2.props.updateData') 。我不知道是什么问题,因为我在互联网上搜索了很多,每个人都像这样向孩子传递道具。请告诉我我错过了什么
家长:
class Parent extends React.Component {
updateData = (data) => {
console.log(`This data isn't parent data. It's ${data}.`)
// data should be 'child data' when the
// Test button in the child component is clicked
}
render() {
return (
<Child updateData={val => this.updateData(val)} />
);
}
孩子:
class Child extends React.Component {
const passedData = 'child data'
handleClick = () => {
this.props.updateData(passedData);
}
render() {
return (
<button onClick={this.handleClick()}>Test</button>
);
}
}
【问题讨论】:
标签: reactjs typescript react-native ecmascript-6