Skip to content

Move the section on file inputs to avoid circular learning dependency #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions content/docs/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select
>```js
><select multiple={true} value={['B', 'C']}>
>```

## 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).
Expand All @@ -197,13 +198,7 @@ In HTML, an `<input type="file">` lets the user choose one or more files from th
<input type="file" />
```

In React, an `<input type="file" />` works similarly to a normal `<input/>` with one important difference: **it is read-only**. (You can't set the value programmatically.) Instead, you should use the File API to interact with the files.

The following example shows how a `ref` can be used to access file(s) in a submit handler:

`embed:forms/input-type-file.js`

[Try it on CodePen](codepen://forms/input-type-file)
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](/docs/uncontrolled-components.html#the-file-input-tag).

## Handling Multiple Inputs

Expand Down
17 changes: 17 additions & 0 deletions content/docs/uncontrolled-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ render() {
```

Likewise, `<input type="checkbox">` and `<input type="radio">` support `defaultChecked`, and `<select>` and `<textarea>` supports `defaultValue`.

## 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`

[Try it on CodePen](codepen://uncontrolled-components/input-type-file)