Skip to content

Commit a4fd8d3

Browse files
authored
Merge pull request #5 from envato/observe-hook-rules
Ensure hooks are always called in the same order
2 parents 7254c09 + 4c9a30a commit a4fd8d3

7 files changed

Lines changed: 18 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<h1 align="center">React Breakpoints</h1>
66

77
<p align="center">
8+
<img alt="dependent repositories" src="https://img.shields.io/librariesio/dependent-repos/npm/@envato/react-breakpoints?label=Used%20by&style=for-the-badge" />
89
<img alt="npm version" src="https://img.shields.io/npm/v/@envato/react-breakpoints?style=for-the-badge" />
910
<img alt="react version" src="https://img.shields.io/npm/dependency-version/@envato/react-breakpoints/dev/react?style=for-the-badge">
1011
<img alt="license" src="https://img.shields.io/npm/l/@envato/react-breakpoints?style=for-the-badge" />

docs/api.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,17 @@ const MyObservedComponent = () => {
490490

491491
# `useResizeObserverEntry()`
492492

493-
⚠️ **Advanced usage** — This hook is used internally in [`useBreakpoints()`](#usebreakpoints) to retrieve the `ResizeObserverEntry` instance set by [`<Observe>`](#observe). It allows you to manually extract [its properties](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry), most notably `.borderBoxSize`, `.contentBoxSize`, and `.devicePixelContentBoxSize`. This hook takes no arguments.
493+
⚠️ **Advanced usage** — This hook is used internally in [`useBreakpoints()`](#usebreakpoints) to retrieve the `ResizeObserverEntry` instance set by [`<Observe>`](#observe). It allows you to manually extract [its properties](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry), most notably `.borderBoxSize`, `.contentBoxSize`, and `.devicePixelContentBoxSize`.
494+
495+
The hook takes an optional `ResizeObserverEntry` as its second argument. **If you pass one, `useResizeObserverEntry()` will not fetch it from the [context](#context), so use caution!**
494496

495497
## Reference guide
496498

497499
```javascript
498-
const resizeObserverEntry = useResizeObserverEntry();
500+
const resizeObserverEntry = useResizeObserverEntry(
501+
/* (optional) a ResizeObserverEntry to use instead of the one provided on context */
502+
injectResizeObserverEntry
503+
);
499504
const fragmentIndex = 0;
500505

501506
/* retrieve width and height from legacy `contentRect` property */

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envato/react-breakpoints",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Respond to changes in a DOM element's size. With React Breakpoints, element queries are no longer \"web design's unicorn\" 🦄",
55
"main": "dist/index.js",
66
"scripts": {

src/Observe.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,19 @@ import { useBreakpoints } from './useBreakpoints';
66
const Observe = ({
77
render,
88
box = undefined,
9-
breakpoints = undefined
9+
breakpoints = {}
1010
}) => {
1111
const observeOptions = box ? { box } : {};
1212

1313
const [ref, observedEntry] = useResizeObserver(observeOptions);
14+
const [widthMatch, heightMatch] = useBreakpoints(breakpoints, observedEntry);
1415

1516
const renderOptions = {
1617
observedElementProps: { ref },
17-
widthMatch: undefined,
18-
heightMatch: undefined
18+
widthMatch,
19+
heightMatch
1920
};
2021

21-
if (breakpoints) {
22-
const [widthMatch, heightMatch] = useBreakpoints(breakpoints, observedEntry);
23-
24-
renderOptions.widthMatch = widthMatch;
25-
renderOptions.heightMatch = heightMatch;
26-
}
27-
2822
return (
2923
<Context.Provider value={observedEntry}>
3024
{render(renderOptions)}

src/useBreakpoints.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const useBreakpoints = ({
4141
box = undefined,
4242
fragment = 0 // https://github.com/w3c/csswg-drafts/pull/4529
4343
}, injectResizeObserverEntry = undefined) => {
44-
const resizeObserverEntry = injectResizeObserverEntry || useResizeObserverEntry();
44+
const resizeObserverEntry = useResizeObserverEntry(injectResizeObserverEntry);
4545

4646
const [width, setWidth] = useState(undefined);
4747
const [height, setHeight] = useState(undefined);

src/useResizeObserverEntry.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { Context } from './Context';
33

44
/**
55
* Returns the ResizeObserverEntry from nearest Context.
6+
* @argument {ResizeObserverEntry} [injectResizeObserverEntry] - Explicitly set the ResizeObserverEntry to use instead of fetching it from Context.
67
* @returns {(ResizeObserverEntry|null)}
78
*/
8-
const useResizeObserverEntry = () => {
9+
const useResizeObserverEntry = injectResizeObserverEntry => {
910
const resizeObserverEntry = useContext(Context);
1011

11-
return resizeObserverEntry;
12+
return injectResizeObserverEntry || resizeObserverEntry;
1213
};
1314

1415
export { useResizeObserverEntry };

0 commit comments

Comments
 (0)