简体   繁体   中英

React pass prop as function then call it error

I have this component A

resetSelectedDates = () => {
    this.setState({
        from_date: null,
        to_date: null
    })
}

render(){
   return(
      <Calendar resetDatePicker={this.resetSelectedDates} />
   )
}

as you can see it passed a function to component B

In component BI call the function like this

    fire(){
        this.props.resetDatePicker.call(null)
    }

<a onClick={this.fire.bind(this)}>Fire</a>

I got error of Cannot read property 'call' of undefined

But in my constructor of component BI did have resetDatePicker when I do console.log

在组件B中试试这个:

<a onClick={() => this.props.resetDatePicker()}>Fire</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM