You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.5 KiB

8 years ago
---
id: uncontrolled-components
title: Uncontrolled Components
permalink: docs/uncontrolled-components.html
---
In most cases, we recommend using [controlled components](/docs/forms.html#controlled-components) to implement forms. In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.
8 years ago
To write an uncontrolled component, instead of writing an event handler for every state update, you can [use a ref](/docs/refs-and-the-dom.html) to get form values from the DOM.
8 years ago
For example, this code accepts a single name in an uncontrolled component:
```javascript{5,9,18}
8 years ago
class NameForm extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.input = React.createRef();
8 years ago
}
handleSubmit(event) {
alert('A name was submitted: ' + this.input.current.value);
8 years ago
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
8 years ago
<label>
Name:
<input type="text" ref={this.input} />
8 years ago
</label>
8 years ago
<input type="submit" value="Submit" />
</form>
);
}
}
```
[**Try it on CodePen**](https://codepen.io/gaearon/pen/WooRWa?editors=0010)
8 years ago
Since an uncontrolled component keeps the source of truth in the DOM, it is sometimes easier to integrate React and non-React code when using uncontrolled components. It can also be slightly less code if you want to be quick and dirty. Otherwise, you should usually use controlled components.
If it's still not clear which type of component you should use for a particular situation, you might find [this article on controlled versus uncontrolled inputs](https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/) to be helpful.
### Default Values {#default-values}
8 years ago
In the React rendering lifecycle, the `value` attribute on form elements will override the value in the DOM. With an uncontrolled component, you often want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a `defaultValue` attribute instead of `value`. Changing the value of `defaultValue` attribute after a component has mounted will not cause any update of the value in the DOM.
8 years ago
8 years ago
```javascript{7}
8 years ago
render() {
return (
<form onSubmit={this.handleSubmit}>
8 years ago
<label>
Name:
<input
defaultValue="Bob"
type="text"
ref={this.input} />
8 years ago
</label>
8 years ago
<input type="submit" value="Submit" />
</form>
);
}
```
Likewise, `<input type="checkbox">` and `<input type="radio">` support `defaultChecked`, and `<select>` and `<textarea>` supports `defaultValue`.
## The file input Tag {#the-file-input-tag}
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](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications).
```html
<input type="file" />
```
In React, an `<input type="file" />` is always an uncontrolled component because its value can only be set by a user, and not programmatically.
You should use the File API to interact with the files. The following example shows how to create a [ref to the DOM node](/docs/refs-and-the-dom.html) to access file(s) in a submit handler:
`embed:uncontrolled-components/input-type-file.js`
[](codepen://uncontrolled-components/input-type-file)