即使我绑定,反应this.props.function也是未定义的

时间:2018-02-13 20:16:08

标签: javascript reactjs bind

我正在努力解决这个问题。 React继续说我的函数未定义但功能正常。

我有一个父组件和一个子组件,我想将一个函数从父传递给子。这就是我所做的:

父母:

    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>
        )
       }
    }

子:

    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>
             )
          }
    }

所以我确实将console.log改为&#34;哇,我找到了你,我得到了这个功能!&#34;。 但是,我也得到了这个:未捕获的TypeError:this.props.thatFun不是函数

有谁知道为什么?谢谢你!

1 个答案:

答案 0 :(得分:0)

代码WORKS。这是一个无关的错误。我有一个孩子的清单,我只需要一个孩子就可以获得这个功能,但我没有正确过滤它。真傻!非常感谢您的关注,代码工作得很好。我将结束这个问题。