[英]TypeError: Cannot read property 'stateUpdate' (Which is a function) of undefined / Passing data from Parent to child
I'm having an issue of passing a function from parent to child component in react js
. 我在react js
有一个功能从父组件传递到子组件。
I have tried to pass the vale to the component as props but it is showing the type error
as defined in the question. 我试图将vale作为道具传递给组件,但它显示了问题中定义的type error
。 i am not able to figure out why is it happening. 我不知道为什么会这样。
Is it because of mapping, if so how to solve it because mapping is important. 是因为映射,如果是因为映射很重要,那么如何解决呢?
Parent Component: 父组件:
stateUpdate(){
alert("hi");
}
render() {
const tabList = this.state.productMapData.map(function(object, i){
return <TableList obj={object} ids = {productRegistrationArrId} key={i}
Error here: 错误在这里:
parent= {this.stateUpdate.bind(this)}></TableList>
})
return (
<div>
<div style={{ marginTop: 10, marginLeft: "10%" }}>
<Link to="/api/productregistration" className="btn btn-info" onClick = {this.onDeleteDb} >
Back to List
</Link>
Child Component: 子组件:
onUpdate(){
this.props.parent;
}
In parent stateUpdate function declare using arrow function then you may not get this issue. 在父stateUpdate函数中使用箭头函数声明,然后可能不会出现此问题。 I have mentioned sample code below please find it 我在下面提到了示例代码,请找到它
stateUpdate =()=> {
alert("hi");
}
and in child component in update function you have to call parent function like 在更新功能的子组件中,您必须调用父函数,例如
onUpdate=()=>{
this.props.parent();
// here you have to change parent to parent()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.