Cannot read property 'target' of undefined

前端 未结 6 1202
感动是毒
感动是毒 2020-12-31 11:03

In my render method I have component


handleChange is fol

相关标签:
6条回答
  • 2020-12-31 11:31

    You are executing function rather than passing it as a prop.

    You need to change this,

     onChange={this.handleChange()}
    

    with this,

     onChange={this.handleChange}
    
    0 讨论(0)
  • 2020-12-31 11:33

    Changing this onChange={this.handleChange} to this onChange={() => this.handleChange()} may also help if all the suggestions above don't work.

    0 讨论(0)
  • 2020-12-31 11:42

    The problem is that you are invoking the function in this line:

    onChange={this.handleChange()}
    

    All you have to do is simply pass the function as a value to onChange without invoking it.

    onChange={this.handleChange}
    

    When you use parenthesis you'd be invoking a function/method instead of binding it with an event handler. Compare the following two examples:

    //invokes handleChange() and stores what the function returns in testA.
    const testA = this.handleChange()  
    

    vs

    //stores the function itself in testB. testB be is now a function and may be invoked - testB();
    const testB = this.handleChange 
    
    0 讨论(0)
  • 2020-12-31 11:45

    You can fix it like this

    function handleChange(event) {
        const value =  event.taget.value;
    }
    
    <input onChange={handleChange} />
    
    not this
    
    <input onChange={handleChange()} />
    
    0 讨论(0)
  • 2020-12-31 11:48

    Simply do this

    <DatePicker selected={this.state.startDate} onChange={this.handleChange}/>
    

    It will pass the event param of the function automatically. The rest of your code is okay.

    0 讨论(0)
  • 2020-12-31 11:54

    If you are passing event using an arrow function, you should try the following:

    onChange={(e) => handleChange(e)}
    
    0 讨论(0)
提交回复
热议问题