-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReact-controlledForm
More file actions
38 lines (38 loc) · 1.12 KB
/
React-controlledForm
File metadata and controls
38 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
submit: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({
// SETS THE INPUT STATE TO THE TYPED INPUT
input: event.target.value
});
}
handleSubmit(event) {
// THE METHOD BELOW PREVENTS THE DEFAULT FORM SUBMIT BEHAVIOR WHICH WILL REFRESH THE WEB PAGE
event.preventDefault();
this.setState({
// SETS THE SUBMIT PROPERTY TO THE INPUT STATE
submit : this.state.input
});
}
render() {
return (
<div>
{ /* THE EVENT HANDLER IS ADDED IN THE FORM ELEMENT AND NOT IN THE SUBMIT B */ }
<form onSubmit={this.handleSubmit}>
{ /* SETS THE INITIAL INPUT VALUE TO THE CLASS STATE AND THEN CHANGES THIS VALUE AS INPUT IS ADDED */ }
<input value = {this.state.input} onChange = {this.handleChange}/>
<button type='submit'>Submit!</button>
</form>
<h1>{this.state.submit}</h1>
</div>
);
}
};