Browse Source

Update Ref API in docs (#970)

* Update Ref API in docs

Update to new React.createRef() API in guide "Uncontrolled Components".

* Update Ref API in doc example

Use new Ref API in guide's example.

* Fix syntax error in example

* Update highlighting ranges in docs

After updating to the new createRef API, the highlighting ranges in Uncontrolled Components were wrong.

* Update highlighting ranges in docs example

After updating to the new createRef API, the highlighting ranges in Uncontrolled Components were wrong.

* Update highlighting ranges in docs example

Remove empty line in source code.

* Update uncontrolled-components.md

* Update input-type-file.js
main
Rodrigo Bermúdez Schettino 7 years ago
committed by Dan Abramov
parent
commit
24f0448d7c
  1. 9
      content/docs/uncontrolled-components.md
  2. 10
      examples/uncontrolled-components/input-type-file.js

9
content/docs/uncontrolled-components.md

@ -10,15 +10,16 @@ To write an uncontrolled component, instead of writing an event handler for ever
For example, this code accepts a single name in an uncontrolled component: For example, this code accepts a single name in an uncontrolled component:
```javascript{8,17} ```javascript{5,9,18}
class NameForm extends React.Component { class NameForm extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.input = React.createRef();
} }
handleSubmit(event) { handleSubmit(event) {
alert('A name was submitted: ' + this.input.value); alert('A name was submitted: ' + this.input.current.value);
event.preventDefault(); event.preventDefault();
} }
@ -27,7 +28,7 @@ class NameForm extends React.Component {
<form onSubmit={this.handleSubmit}> <form onSubmit={this.handleSubmit}>
<label> <label>
Name: Name:
<input type="text" ref={(input) => this.input = input} /> <input type="text" ref={this.input} />
</label> </label>
<input type="submit" value="Submit" /> <input type="submit" value="Submit" />
</form> </form>
@ -55,7 +56,7 @@ render() {
<input <input
defaultValue="Bob" defaultValue="Bob"
type="text" type="text"
ref={(input) => this.input = input} /> ref={this.input} />
</label> </label>
<input type="submit" value="Submit" /> <input type="submit" value="Submit" />
</form> </form>

10
examples/uncontrolled-components/input-type-file.js

@ -1,13 +1,15 @@
class FileInput extends React.Component { class FileInput extends React.Component {
// highlight-range{4}
constructor(props) { constructor(props) {
super(props); super(props);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.fileInput = React.createRef();
} }
// highlight-range{4} // highlight-range{4}
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
alert( alert(
`Selected file - ${this.fileInput.files[0].name}` `Selected file - ${this.fileInput.current.files[0].name}`
); );
} }
@ -16,12 +18,10 @@ class FileInput extends React.Component {
<form onSubmit={this.handleSubmit}> <form onSubmit={this.handleSubmit}>
<label> <label>
Upload file: Upload file:
{/* highlight-range{1-6} */} {/* highlight-range{1-3} */}
<input <input
type="file" type="file"
ref={input => { ref={this.fileInput}
this.fileInput = input;
}}
/> />
</label> </label>
<br /> <br />

Loading…
Cancel
Save