-1

The following code

import React, { Component } from 'react';

class AddNinja extends Component {
  state = {
    name: null,
    age: null,
    belt: null
  }
  handleChange = (e) => {
    this.setState({[e.target.id]: e.taget.value});
  }
  handleSubmit = (e) => {
    e.preventDefault();  
    console.log(this.state);
  }
  render() {
    return (
      <div>        
        <form onSubmit={this.handleSubmit}> 
          <label htmlFor='name'>Name: </label>
          <input type='text' id='name' onChange={this.handleChange} />
          <label htmlFor='age'>Age: </label>
          <input type='text' id='age' onChange={this.handleChange} />
          <label htmlFor='belt'>Belt: </label>
          <input type='text' id='belt' onChange={this.handleChange} />
          <button>Submit</button>
        </form>
      </div>
    )
  }
}

export default AddNinja;

Will triger the error below;

TypeError: Cannot read property 'value' of undefined
AddNinja._this.handleChange
src/AddNinja.jsx:10
   7 |   belt: null
   8 | }
   9 | handleChange = (e) => {
> 10 |   this.setState({[e.target.id]: e.taget.value});
  11 | }
  12 | handleSubmit = (e) => {
  13 |   e.preventDefault();  

When I explicitly use any of the three defined key it will work i.e:

this.setState({name: e.taget.value});

Antbody knows what going on? According to the following link it should work

Fabou78
  • 169
  • 1
  • 2
  • 12

1 Answers1

1

You've misspelled target and used taget instead. Try this:

this.setState({[e.target.id]: e.target.value});
Smarticles101
  • 1,822
  • 1
  • 14
  • 28