re-act giving error

Asked

Viewed 31 times

-1

I am making a form in React and this giving this error

'''

    '''
TypeError: Cannot read property 'name' of undefined
UserCrud.updateField

  48 | 
  49 | updateField(event) {
  50 |   const user = { ...this.state.user};
> 51 |   user[user.target.name] = event.target.value;  /* usa nome do input procura propriedade ,diz os atributos ,setou valor do input*/
  52 |   this.setState({ user }) /* passa user direto do ecma 2015 */
  53 | 
  54 | } 
View compiled
onChange

    

  63 |     <input type="text" className="form-control"
  64 |     name="name"
  65 |     value={this.state.user.name}
> 66 |     onChange={e => this.updateField(e)}
     | ^  67 |     placeholder="Digite o nome ..."/>
  68 |     
  69 | </div>
View compiled 
'''

1 answer

0


The error must be on this line

user[user.target.name] = event.target.value;  /* usa nome do input procura propriedade ,diz os atributos ,setou valor do input*/

The function is not receiving a "user" parameter, the parameter name is "Event". "user" is the variable that stores the value of the state "user, inside it there is no "target".

Change to:

user[event.target.name] = event.target.value;  /* usa nome do input procura propriedade ,diz os atributos ,setou valor do input*/
  • Thank you brother was smashing his head

Browser other questions tagged

You are not signed in. Login or sign up in order to post.