Last active
September 1, 2018 20:02
-
-
Save negamaxi/49c41faed81df7811f917b676d54dd32 to your computer and use it in GitHub Desktop.
Simple React forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// External state example (Redux e.g) | |
class SimplerReactForm extends React.Component { | |
handleInputChange = (e) => { | |
const { name, value } = e.target | |
this.props.onChange({ | |
...this.props.values, | |
[name]: value | |
}) | |
} | |
handleSubmit = () => { | |
// do stuff with this.props.values | |
} | |
render() { | |
return( | |
<form onSubmit={this.handleSubmit}> | |
<input | |
name="login" | |
type="text" | |
placeholder="login" | |
value={this.props.values.login} | |
onChange={this.handleChange} | |
/> | |
<input | |
name="password" | |
type="password" | |
placeholder="password" | |
value={this.props.values.password} | |
onChange={this.handleChange} | |
/> | |
<button> | |
submit | |
</button> | |
</form> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Local state example | |
class SimplerReactForm extends React.Component { | |
state = { | |
login: "", | |
password: "" | |
} | |
handleInputChange = (e) => { | |
const { name, value } = e.target | |
this.setState({ | |
[name]: value | |
}) | |
} | |
handleSubmit = () => { | |
// do stuff with this.state | |
} | |
render() { | |
return( | |
<form onSubmit={this.handleSubmit}> | |
<input | |
name="login" | |
type="text" | |
placeholder="login" | |
value={this.state.login} | |
onChange={this.handleChange} | |
/> | |
<input | |
name="password" | |
type="password" | |
placeholder="password" | |
value={this.state.password} | |
onChange={this.handleChange} | |
/> | |
<button> | |
submit | |
</button> | |
</form> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uncontrolled form example | |
class SimplerReactForm extends React.Component { | |
handleSubmit = e => { | |
e.preventDefault(); | |
const formData = new FormData(e.target); | |
// you can submit it like that | |
// or you can covert it to a json | |
const jsonData = Array | |
.from(formData) | |
.reduce((acc, [name, value]) => { | |
acc[name] = value; | |
return acc | |
}, {}) | |
}; | |
render() { | |
return ( | |
<form onSubmit={this.handleSubmit}> | |
<input | |
name="login" | |
type="text" | |
placeholder="login" | |
/> | |
<input | |
name="password" | |
type="password" | |
placeholder="password" | |
/> | |
<button> | |
submit | |
</button> | |
</form> | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment