Skip to content

Commit 6f5086e

Browse files
Merge branch 'master' of https://github.com/reactjs/reactjs.org into sync-fa5e6e7a
2 parents a924421 + fa5e6e7 commit 6f5086e

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

content/docs/faq-ajax.md

+47
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,50 @@ class MyComponent extends React.Component {
8282
}
8383
}
8484
```
85+
86+
Here is the equivalent with [Hooks](https://reactjs.org/docs/hooks-intro.html):
87+
88+
```js
89+
function MyComponent() {
90+
const [error, setError] = useState(null);
91+
const [isLoaded, setIsLoaded] = useState(false);
92+
const [items, setItems] = useState([]);
93+
94+
// Note: the empty deps array [] means
95+
// this useEffect will run once
96+
// similar to componentDidMount()
97+
useEffect(() => {
98+
fetch("https://api.example.com/items")
99+
.then(res => res.json())
100+
.then(
101+
(result) => {
102+
setIsLoaded(true);
103+
setItems(result.items);
104+
},
105+
// Note: it's important to handle errors here
106+
// instead of a catch() block so that we don't swallow
107+
// exceptions from actual bugs in components.
108+
(error) => {
109+
setIsLoaded(true);
110+
setError(error);
111+
}
112+
)
113+
}, [])
114+
115+
if (error) {
116+
return <div>Error: {error.message}</div>;
117+
} else if (!isLoaded) {
118+
return <div>Loading...</div>;
119+
} else {
120+
return (
121+
<ul>
122+
{items.map(item => (
123+
<li key={item.name}>
124+
{item.name} {item.price}
125+
</li>
126+
))}
127+
</ul>
128+
);
129+
}
130+
}
131+
```

content/docs/forms.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,7 @@ class NameForm extends React.Component {
6868

6969
Since the `value` attribute is set on our form element, the displayed value will always be `this.state.value`, making the React state the source of truth. Since `handleChange` runs on every keystroke to update the React state, the displayed value will update as the user types.
7070

71-
With a controlled component, every state mutation will have an associated handler function. This makes it straightforward to modify or validate user input. For example, if we wanted to enforce that names are written with all uppercase letters, we could write `handleChange` as:
72-
73-
```javascript{2}
74-
handleChange(event) {
75-
this.setState({value: event.target.value.toUpperCase()});
76-
}
77-
```
71+
With a controlled component, the input's value is always driven by the React state. While this means you have to type a bit more code, you can now pass the value to other UI elements too, or reset it from other event handlers.
7872

7973
## The textarea Tag {#the-textarea-tag}
8074

content/docs/strict-mode.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Because the above methods might be called more than once, it's important that th
9999

100100
Strict mode can't automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:
101101

102-
* Class component `constructor`, `render`, and `shouldComponent` methods
102+
* Class component `constructor`, `render`, and `shouldComponentUpdate` methods
103103
* Class component static `getDerivedStateFromProps` method
104104
* Function component bodies
105105
* State updater functions (the first argument to `setState`)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class FileInput extends React.Component {
66
this.fileInput = React.createRef();
77
}
88
handleSubmit(event) {
9-
// highlight-range{4}
9+
// highlight-range{3}
1010
event.preventDefault();
1111
alert(
1212
`Selected file - ${this.fileInput.current.files[0].name}`

0 commit comments

Comments
 (0)