Ghadir
Ghadir

Reputation: 537

React Form Field on change function not passing target value to setState

This code could use some improvement, but for now, is there a reason why the name property inside setState cannot access the event e parameter?

//keyObject is a predefined variable

<Form.Control
  type="text"
  value={this.state.productObject[keyObject].name || ""}
  placeholder={"name"}
  onChange={(e) => {
    this.setState(function (prevState) {
      return {
        ...prevState,
        productObject: {
          ...prevState.productObject,
          [keyObject]: {
            name: e.target.value,
            price: prevState.productObjecy[keyObject].price,
            selected: prevState.productObject[keyObject].selected,
          },
        },
      };
    });
  }}
/>;

When I attempt to change the value of the field, I get the 'Cannot read property 'value' of null' error.

Upvotes: 0

Views: 20

Answers (1)

theabrar
theabrar

Reputation: 440

Have you tried

e.target.name.value

An easy way to debug this is to do console.log(e.target) and see what you need next.

Upvotes: 1

Related Questions