Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
905a113
Add React Rally 2019 (#1840)
BrianMitchL Mar 18, 2019
806da34
Add a DOM measurement recipe to Hooks FAQ (#1843)
gaearon Mar 19, 2019
ac4aa65
Add more explanations to Hooks API page (#1845)
gaearon Mar 19, 2019
b1bc193
Document useContext bailout strategy (#1848)
gaearon Mar 19, 2019
1f2dbb7
Update languages for Simplified Chinese completion (#1854)
tesseralis Mar 20, 2019
aad51f2
merging all conflicts
reactjs-translation-bot Mar 22, 2019
9bfaa3d
[ru.reactjs.org] fix sync conflicts
gcor Mar 23, 2019
11001c0
fix after review
gcor Mar 30, 2019
b5fd7b9
fix after review
gcor Mar 30, 2019
7e502fb
fix after review
gcor Mar 30, 2019
a22c4b5
Merge pull request #286 from gcor/sync-1f2dbb7a
gcor Apr 1, 2019
0f15d66
Update content/docs/hooks-reference.md
gcor Apr 1, 2019
bc33957
Update content/docs/hooks-reference.md
gcor Apr 1, 2019
4ae90f6
Update content/docs/hooks-faq.md
lex111 Apr 2, 2019
c50331e
Update content/docs/hooks-faq.md
lex111 Apr 2, 2019
2d37a5e
Update content/docs/hooks-faq.md
lex111 Apr 2, 2019
ba24f1d
Update content/docs/hooks-reference.md
lex111 Apr 2, 2019
42d54fd
Update content/docs/hooks-reference.md
lex111 Apr 2, 2019
402c0a9
Update content/docs/hooks-reference.md
lex111 Apr 2, 2019
3cf2d73
Update content/docs/hooks-faq.md
lex111 Apr 2, 2019
cfb5e29
Update content/docs/hooks-faq.md
lex111 Apr 2, 2019
6a4edbb
Update content/docs/hooks-reference.md
lex111 Apr 2, 2019
b6c974c
Update content/docs/hooks-reference.md
gcor Apr 2, 2019
3b7df89
Apply suggestions from code review
lex111 Apr 2, 2019
ce39f1d
Update content/docs/hooks-reference.md
gcor Apr 2, 2019
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
Prev Previous commit
Next Next commit
Add a DOM measurement recipe to Hooks FAQ (#1843)
* Add a DOM measurement recipe to Hooks FAQ

* Update content/docs/hooks-faq.md

Co-Authored-By: gaearon <dan.abramov@gmail.com>

* Update content/docs/hooks-faq.md

Co-Authored-By: gaearon <dan.abramov@gmail.com>
  • Loading branch information
gaearon authored Mar 19, 2019
commit 806da34988106c2a92c9784edc2bc0e91333ca4a
55 changes: 55 additions & 0 deletions content/docs/hooks-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This page answers some of the frequently asked questions about [Hooks](/docs/hoo
* [How do I implement getDerivedStateFromProps?](#how-do-i-implement-getderivedstatefromprops)
* [Is there something like forceUpdate?](#is-there-something-like-forceupdate)
* [Can I make a ref to a function component?](#can-i-make-a-ref-to-a-function-component)
* [How can I measure a DOM node?](#how-can-i-measure-a-dom-node)
* [What does const [thing, setThing] = useState() mean?](#what-does-const-thing-setthing--usestate-mean)
* **[Performance Optimizations](#performance-optimizations)**
* [Can I skip an effect on updates?](#can-i-skip-an-effect-on-updates)
Expand Down Expand Up @@ -451,6 +452,60 @@ Try to avoid this pattern if possible.

While you shouldn't need this often, you may expose some imperative methods to a parent component with the [`useImperativeHandle`](/docs/hooks-reference.html#useimperativehandle) Hook.

### How can I measure a DOM node? {#how-can-i-measure-a-dom-node}

In order to measure the position or size of a DOM node, you can use a [callback ref](/docs/refs-and-the-dom.html#callback-refs). React will call that callback whenever the ref gets attached to a different node. Here is a [small demo](https://codesandbox.io/s/l7m0v5x4v9):

```js{4-8,12}
function MeasureExample() {
const [height, setHeight] = useState(0);

const measuredRef = useCallback(node => {
if (node !== null) {
setHeight(node.getBoundingClientRect().height);
}
}, []);

return (
<>
<h1 ref={measuredRef}>Hello, world</h1>
<h2>The above header is {Math.round(height)}px tall</h2>
</>
);
}
```

We didn't choose `useRef` in this example because an object ref doesn't notify us about *changes* to the current ref value. Using a callback ref ensures that [even if a child component displays the measured node later](https://codesandbox.io/s/818zzk8m78) (e.g. in response to a click), we still get notified about it in the parent component and can update the measurements.

Note that we pass `[]` as a dependency array to `useCallback`. This ensures that our ref callback doesn't change between the re-renders, and so React won't call it unnecessarily.

If you want, you can [extract this logic](https://codesandbox.io/s/m5o42082xy) into a reusable Hook:

```js{2}
function MeasureExample() {
const [rect, ref] = useClientRect();
return (
<>
<h1 ref={ref}>Hello, world</h1>
{rect !== null &&
<h2>The above header is {Math.round(rect.height)}px tall</h2>
}
</>
);
}

function useClientRect() {
const [rect, setRect] = useState(null);
const ref = useCallback(node => {
if (node !== null) {
setRect(node.getBoundingClientRect());
}
}, []);
return [rect, ref];
}
```


### What does `const [thing, setThing] = useState()` mean? {#what-does-const-thing-setthing--usestate-mean}

If you're not familiar with this syntax, check out the [explanation](/docs/hooks-state.html#tip-what-do-square-brackets-mean) in the State Hook documentation.
Expand Down