Skip to content Skip to sidebar Skip to footer

Handling Multiple Inputs In React

I am new to React and was learning how to handle multiple inputs in form. Here is the code : class Login extends Component { constructor () { super(); this.state = {

Solution 1:

[evt.target.name] doesn't necessarily represent both inputs, it merely takes the name of the event's target and makes it the key for setState.

This means that when the email form changes, the this.setState will act as follows:

this.setState({ email: evt.target.value });

For the password this works the same;

this.setState({ password: evt.target.value });

So it doesn't necessarily replace several handlers, it mostly replaces them and supplies a shorter way to handle the event. (Think DNRY (Do Not Repeat Yourself)).

However many fields you have in the form, this.setState({ [evt.target.name]: evt.target.value }); will behave as explained above.

To further elaborate, in your current form, with a field for the email and a field for the password, the following will happen;

handleChange (evt) {
    this.setState({ [evt.target.name]: evt.target.value });
}

Above function will take the event's target and extract the name and value attributes. So for EACH input with this change handler it'll change the function to i.e. the following if the email gets changed;

handleChange (evt) {
    this.setState({ email: "email@domain.com" });
}

OR i.e. for the password

handleChange (evt) {
    this.setState({ password: "superstrongpassword" });
}

OR i.e. for the name

handleChange (evt) {
    this.setState({ name: "John Doe" });
}

Hope this helps!

Solution 2:

This is basic example.

classLoginextendsComponent{

  constructor () {
    super();
    this.state = {
      email: '',
      password: ''
    };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange (evt, field) {
    // check it out: we get the evt.target.name (which will be either "email" or "password")// and use it to target the key on our `state` object with the same name, using bracket syntaxthis.setState({ [field]: evt.target.value });
  }

  render () {
    return (
  <form>

    <label>Email</label>
    <input type="text" name="email" onChange={(event)=>this.handleChange(event, "email")} />

    <label>Password</label>
    <input type="password" name="password" onChange={(event)=>this.handleChange(event, "password")} />

  </form>
);
  }
}

Solution 3:

This works because evt is a change event, evt.target is the changed DOM element and evt.target.name is the value of the name attribute of that element.

This means that when you change one of the <input> elements the handleChange function is called and the state's email or password (the names of those two inputs) property is changed to the changed element's value.

{[variable]: value} is just the syntax you use when you want to use a string as a property name in an object literal.

Solution 4:

Every input has a name attribute which is used to reference elements in JavaScript, or to reference form data after a form is submitted. Here you are using name and password as names.

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

This is using ES6 Computed property names. evt.target.name takes the name of the input (i.e the evt.target) to which the handler is attached and being called and sets it as the key for your state. So, essentialy each time you change something in the input, your state corresponding to that input's name changes as well.

Keep one thing in mind. You need to keep the initial state names and the names of your input consistent with each other for this to work.

You can always write seperate handlers but that just convolutes the codebase and are essentially doing the same thing. So it is just following the DRY methodology. Might as well use this.

Hope this helps!

Solution 5:

for create a state for this, is necessary create a object with any property and set

just behaivor on component an above class with your scheme class and component this is trigger for you response

  1. Create an object property

    state = {
     nameForProperty: '',
     anotherNamenameForProperty: ''
    }
    
  2. create an event for setState a pass event for function callback onHandleEvent = e => {

    this.setState ({
             [e.target.name]: e.target.value,
            })
               console.log(this.setState)
    
           };
    
  3. Call function from input

    <form><inputname="namenameForProperty"onChange={this.onHandleEvent}value={this.state.} /></form>

Post a Comment for "Handling Multiple Inputs In React"