[英]react this.props.function is undefined even I bind
I am pretty much struggling with this.我在这方面很挣扎。 React keep saying my function is undefined but the function is working. React 一直说我的函数未定义,但该函数正在运行。
I have a parent component and a child component, I want to pass down a function from parent to child.我有一个父组件和一个子组件,我想将一个函数从父组件传递给子组件。 This is what I did:这就是我所做的:
parent :家长:
import React, { Component } from 'react';
import { Child } from 'components';
export default class Parent extends Component{
constructor(props){
super(props);
this.myFunc = this.myFunc.bind(this);
}
myFunc (data) {
console.log("wowow I got you!", data)
}
render(){
return (
<div>
<Child thatFunc={this.myFunc} />
</div>
)
}
}
child:孩子:
import React, { Component } from 'react';
export default class Child extends Component {
constructor(props) {
super(props);
}
componentDidMount(){
this.props.thatFunc("i got the function!")
}
render(){
return (
<div> do this do that </div>
)
}
}
So I do get the console.log correct as " wowow I got you i got the function!".所以我确实得到了 console.log 正确的“哇,我得到了你,我得到了这个功能!”。 But, I got this as well: Uncaught TypeError: this.props.thatFun is not a function但是,我也得到了这个:Uncaught TypeError: this.props.thatFun is not a function
Does anyone knows why?Thankkkk you!有谁知道为什么?谢谢你!
The code WORKS.代码有效。 It was an unrelated bug.这是一个无关的错误。 I have a list of children and I just need ONE child to be able to get this function, but I didn't filter it properly.我有一个孩子的列表,我只需要一个孩子就能获得这个功能,但我没有正确过滤它。 How silly!多么愚蠢! Thank you so much for your attention, the code works just fine.非常感谢您的关注,代码工作正常。 I will close this question.我将关闭这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.