1

I have a react state like:

this.state = {
  formInputs: {
    username: '',
    password: '',
    passwordConfirm: '',
  },
  handleChange = () => {
    const {target: {name, value}} = event;
    this.setState({
      [name as keyof formInputs]: value
    });
  }
};

How can I change this line ( [name as keyof formData]: value) to a JavaScript instead of Typescript?

1

3 Answers 3

4

We can use Computed property names concept to compute the object property name dynamically. For that we need to put the expression inside [].

When you need to handle multiple controlled input elements, you can add a name attribute to each element and let the handler function choose what to do based on the value of event.target.name.

For your state

this.setState({
    formInput: {
        ...this.state.formInput,
        [event.target.name]: event.target.value
    }
})

Sandbox for your reference: https://codesandbox.io/s/react-basic-example-p7ft8

import React, { Component } from "react";

export default class Login extends Component {
  state = {
    formInputs: {
      email: "",
      password: ""
    }
  };

  handleOnChange = event => {
    this.setState({
      formInput: {
        ...this.state.formInput,
        [event.target.name]: event.target.value
      }
    });
  };

  render() {
    return (
      <form>
        <label>Email</label>
        <input type="text" name="email" onChange={this.handleOnChange} />
        <label>Password</label>
        <input type="password" name="password" onChange={this.handleOnChange} />
      </form>
    );
  }
}


Sign up to request clarification or add additional context in comments.

3 Comments

what if you have form state object like: state = { formInput: { username:"", password: "" } would you still set it like this handleOnChange = event => { this.setState({ [event.target.name]: event.target.value }) }
I made a change to my answer for this question
I think my edit is also identical to what @Burak Gavas answered
2

You can directly use Bracket_notation

[name]: value

In your case, { formInput: { username:"", password: "" }

this.setState({
    formInput: {
        ...this.state.formInput,
        [name]: value
    }
});

2 Comments

I have tried that but it is binding the form data. for example I console the form state values are still null.
I think that you should put your handleChange function out of your state
0

I think you can initialize your formObject as empty json and

this.state = {formInput: {}}

Later onChange you can set the value something like

this.setState({formInput[event.target.name]: event.target.value})

and conditionaly check if (this.state.formInput.username) ? this.state.formInput.username : '' to value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.