Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React change input value onChange

This is my SearchForm.js, handleKeywordsChange must handle input keywords changes

import React from 'react';
import ReactDOM from 'react-dom';

class SearchForm extends React.Component {
    constructor(props) {
      super(props)

      this.state = {
       keywords: '',
       city: '',
       date: ''     
      }

      //this.handleChange = this.handleChange.bind(this)
      //this.handleSubmit = this.handleSubmit.bind(this)

      this.handleKeywordsChange = this.handleKeywordsChange.bind(this);
     }

    handleKeywordsChange(e) {
        console.log(1);

        this.setState({
          value: e.target.value
        });
    }

    render() {
        return ( 
            <form className='form search-form' onSubmit={this.handleSubmit}>
                <div className="form-row">
                  <div className="form-group col-md-5">
                    <label htmlFor="keywords">Keywords</label>
                    <input type="text" className="form-control" name="keywords" id="keywords" placeholder="Keywords" onChange={this.handleKeywordsChange} value={this.state.keywords} />

                  </div>

                  <div className="form-group col-md-5">
                    <label htmlFor="city">City</label>
                    <input type="text" className="form-control" name="city" id="city" placeholder="City" onChange={this.handleChange} value={this.state.city} />
                  </div>

                  <div className="form-group col-md-2">
                    <label htmlFor="date">Date</label>
                    <select className="form-control" name="date" id="date" onChange={this.handleChange} value={this.state.date}>
                      <option>1</option>
                      <option>2</option>
                      <option>3</option>
                      <option>4</option>
                      <option>5</option>
                    </select>
                  </div>
                 </div>

                 <div className="form-row">
                     <div className="form-group col-md-12">
                        <input id='formButton' className='btn btn-primary' type='submit' placeholder='Send' />
                    </div>
                 </div>
            </form>
        )
    }
}

export { SearchForm }

The problem is input keywords doesn't change its value when I'm typing. What's wrong?

like image 597
Heidel Avatar asked Mar 19 '18 11:03

Heidel


People also ask

How do I change my onChange value in React?

To modify the value of a textarea using onChange in React: Add an onChange prop to the textarea setting it to a function. Store the value of the textarea in the state via the useState hook. Update the state variable every time the user types into the textarea.

How do I change input type in React?

In React Js, I can use a state variable to change the input type of an input element. I just use a state variable to hold the input type like <input type={inputType} /> . When I assign a valid input type to the inputType state variable, the input element will change to that type.


1 Answers

Make a common function for changing the state for input values.

handleInputChange(e) {
    this.setState({
        [e.target.name]: e.target.value
    });
}

Make sure you mention name in every input tag. e.g:

<input name="someUniqueName" value={this.state.someState} onChange={this.handleInputChange} />
like image 119
Amanshu Kataria Avatar answered Sep 20 '22 14:09

Amanshu Kataria