You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add more explanations to API page
* static
* add info about useLayoutEffect
* Update content/docs/hooks-reference.md
Co-Authored-By: gaearon <[email protected]>
* nits
* nit
If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects. (React uses the [`Object.is` comparison algorithm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Description).)
99
99
100
+
Note that React may still need to render that specific component again before bailing out. That shouldn't be a concern because React won't unnecessarily go "deeper" into the tree. If you're doing expensive calculations while rendering, you can optimize them with `useMemo`.
101
+
100
102
### `useEffect` {#useeffect}
101
103
102
104
```js
@@ -173,12 +175,24 @@ The array of dependencies is not passed as arguments to the effect function. Con
173
175
### `useContext` {#usecontext}
174
176
175
177
```js
176
-
constcontext=useContext(Context);
178
+
constvalue=useContext(MyContext);
177
179
```
178
180
179
-
Accepts a context object (the value returned from `React.createContext`) and returns the current context value, as given by the nearest context provider for the given context.
181
+
Accepts a context object (the value returned from `React.createContext`) and returns the current context value for that context. The current context value is determined by the `value` prop of the nearest `<MyContext.Provider>` above the calling component in the tree.
182
+
183
+
When the nearest `<MyContext.Provider>` above the component updates, this Hook will trigger a rerender with the latest context `value` passed to that `MyContext` provider.
180
184
181
-
When the provider updates, this Hook will trigger a rerender with the latest context value.
185
+
Don't forget that the argument to `useContext` must be the *context object itself*:
186
+
187
+
***Correct:**`useContext(MyContext)`
188
+
***Incorrect:**`useContext(MyContext.Consumer)`
189
+
***Incorrect:**`useContext(MyContext.Provider)`
190
+
191
+
>Tip
192
+
>
193
+
>If you're familiar with the context API before Hooks, `useContext(MyContext)` is equivalent to `static contextType = MyContext` in a class, or to `<MyContext.Consumer>`.
194
+
>
195
+
>`useContext(MyContext)` only lets you *read* the context and subscribe to its changes. You still need a `<MyContext.Provider>` above in the tree to *provide* the value for this context.
182
196
183
197
## Additional Hooks {#additional-hooks}
184
198
@@ -285,6 +299,8 @@ function Counter({initialCount}) {
285
299
286
300
If you return the same value from a Reducer Hook as the current state, React will bail out without rendering the children or firing effects. (React uses the [`Object.is` comparison algorithm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Description).)
287
301
302
+
Note that React may still need to render that specific component again before bailing out. That shouldn't be a concern because React won't unnecessarily go "deeper" into the tree. If you're doing expensive calculations while rendering, you can optimize them with `useMemo`.
303
+
288
304
### `useCallback` {#usecallback}
289
305
290
306
```js
@@ -356,7 +372,16 @@ function TextInputWithFocusButton() {
356
372
}
357
373
```
358
374
359
-
Note that `useRef()` is useful for more than the `ref` attribute. It's [handy for keeping any mutable value around](/docs/hooks-faq.html#is-there-something-like-instance-variables) similar to how you'd use instance fields in classes.
375
+
Essentially, `useRef` is like a "box" that can hold a mutable value in its `.current` property.
376
+
377
+
You might be familiar with refs primarily as a way to [access the DOM](/docs/refs-and-the-dom.html). If you pass a ref object to React with `<div ref={myRef} />`, React will set its `.current` property to the corresponding DOM node whenever that node changes.
378
+
379
+
However, `useRef()` is useful for more than the `ref` attribute. It's [handy for keeping any mutable value around](/docs/hooks-faq.html#is-there-something-like-instance-variables) similar to how you'd use instance fields in classes.
380
+
381
+
This works because `useRef()` creates a plain JavaScript object. The only difference between `useRef()` and creating a `{current: ...}` object yourself is that `useRef` will give you the same ref object on every render.
382
+
383
+
Keep in mind that `useRef`*doesn't* notify you when its content changes. Mutating the `.current` property doesn't cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use a [callback ref](/docs/hooks-faq.html#how-can-i-measure-a-dom-node) instead.
384
+
360
385
361
386
### `useImperativeHandle` {#useimperativehandle}
362
387
@@ -389,7 +414,11 @@ Prefer the standard `useEffect` when possible to avoid blocking visual updates.
389
414
390
415
> Tip
391
416
>
392
-
> If you're migrating code from a class component, `useLayoutEffect` fires in the same phase as `componentDidMount` and `componentDidUpdate`, so if you're unsure of which effect Hook to use, it's probably the least risky.
417
+
> If you're migrating code from a class component, note `useLayoutEffect` fires in the same phase as `componentDidMount` and `componentDidUpdate`. However, **we recommend starting with `useEffect` first** and only trying `useLayoutEffect` if that causes a problem.
418
+
>
419
+
>If you use server rendering, keep in mind that *neither*`useLayoutEffect` nor `useEffect` can run until the JavaScript is downloaded. This is why React warns when a server-rendered component contains `useLayoutEffect`. To fix this, either move that logic to `useEffect` (if it isn't necessary for the first render), or delay showing that component until after the client renders (if the HTML looks broken until `useLayoutEffect` runs).
420
+
>
421
+
>To exclude a component that needs layout effects from the server-rendered HTML, render it conditionally with `showChild && <Child />` and defer showing it with `useEffect(() => { setShowChild(true); }, [])`. This way, the UI doesn't appear broken before hydration.
0 commit comments