Accessing this.props function in react ComponentDidMount

时间:2017-09-22 07:58:24

标签: javascript reactjs

I am trying to access parent function (passed as property to child) in child's componentDidMount() but it is showing as undefined.

constructor(props) {
    super(props)
    this.actionis = this.props.actionis.bind(this);
}

componentDidMount(){
    this.actionis(selectednow, 'disabled');
}

EDIT: Parent Constructor:

constructor(props) {
        super(props)
        var abc = "";
        this.handlerHeading = this.handlerHeading.bind(this)
        this.handlerHeadingRemove = this.handlerHeadingRemove.bind(this)
     }

Parent Render():

<Mappingcomp actionis={this.handlerHeading} />

handlerHeading Function:

handlerHeading(index, disabled) {
        xarraydis.push(index);
    }

Error Is:

this.actionis is not a function

But i can access it in render() function and not in componentDidMount().

2 个答案:

答案 0 :(得分:1)

您无需绑定@Modifying @Query("update Notification msg set msg.message = ?1 where msg.applicationUser.id = ?2") int createNewNotification(String message, Long id); 回调函数。

只做

prop

答案 1 :(得分:0)

您只需要使用类似

的函数绑定父级中返回的参数
<Mappingcomp actionis={(index, disabled) => this.handlerHeading(index, disabled)} />

然后在孩子使用

constructor(props) {
    super(props)
}

componentDidMount(){
    this.props.actionis(selectednow, 'disabled');
}