In HTML, form elements such as <input>
, <textarea>
, and <select>
typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState().
For example, if we want to make the previous example log the name when it is submitted, we can write the form as a controlled component:
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); `}`
handleChange(event) {
`this.setState({value: event.target.value});`
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
`); `}` `}`
In HTML, a <textarea>
element defines its text by its children
In React, a <textarea>
uses a value attribute instead. This way, a form using a <textarea>
can be written very similarly to a form that uses a single-line input
In HTML, <select>
creates a drop-down list. For example, this HTML creates a drop-down list of flavors
React, instead of using this selected attribute, uses a value attribute on the root select tag. This is more convenient in a controlled component because you only need to update it in one place.
In HTML, an <input type="file">
lets the user choose one or more files from their device storage to be uploaded to a server or manipulated by JavaScript via the File API.
Because its value is read-only, it is an uncontrolled component in React. It is discussed together with other uncontrolled components later in the documentation.
consider this code :
if ( condition ) {
value if true;
}
else {
value if false;
}
using the Ternary Operator, the code will be refactores as :
condition ? value if true : value if false
Here’s what you need to know: