This is my SearchForm.js, handleKeywordsChange
must handle input keywords
changes
JavaScript
x
66
66
1
import React from 'react';
2
import ReactDOM from 'react-dom';
3
4
class SearchForm extends React.Component {
5
constructor(props) {
6
super(props)
7
8
this.state = {
9
keywords: '',
10
city: '',
11
date: ''
12
}
13
14
//this.handleChange = this.handleChange.bind(this)
15
//this.handleSubmit = this.handleSubmit.bind(this)
16
17
this.handleKeywordsChange = this.handleKeywordsChange.bind(this);
18
}
19
20
handleKeywordsChange(e) {
21
console.log(1);
22
23
this.setState({
24
value: e.target.value
25
});
26
}
27
28
render() {
29
return (
30
<form className='form search-form' onSubmit={this.handleSubmit}>
31
<div className="form-row">
32
<div className="form-group col-md-5">
33
<label htmlFor="keywords">Keywords</label>
34
<input type="text" className="form-control" name="keywords" id="keywords" placeholder="Keywords" onChange={this.handleKeywordsChange} value={this.state.keywords} />
35
36
</div>
37
38
<div className="form-group col-md-5">
39
<label htmlFor="city">City</label>
40
<input type="text" className="form-control" name="city" id="city" placeholder="City" onChange={this.handleChange} value={this.state.city} />
41
</div>
42
43
<div className="form-group col-md-2">
44
<label htmlFor="date">Date</label>
45
<select className="form-control" name="date" id="date" onChange={this.handleChange} value={this.state.date}>
46
<option>1</option>
47
<option>2</option>
48
<option>3</option>
49
<option>4</option>
50
<option>5</option>
51
</select>
52
</div>
53
</div>
54
55
<div className="form-row">
56
<div className="form-group col-md-12">
57
<input id='formButton' className='btn btn-primary' type='submit' placeholder='Send' />
58
</div>
59
</div>
60
</form>
61
)
62
}
63
}
64
65
export { SearchForm }
66
The problem is input keywords
doesn’t change its value when I’m typing. What’s wrong?
Advertisement
Answer
Make a common function for changing the state for input values.
JavaScript
1
6
1
handleInputChange(e) {
2
this.setState({
3
[e.target.name]: e.target.value
4
});
5
}
6
Make sure you mention name
in every input
tag. e.g:
JavaScript
1
2
1
<input name="someUniqueName" value={this.state.someState} onChange={this.handleInputChange} />
2