|
5 | 5 | <h1 align="center">React Breakpoints</h1> |
6 | 6 |
|
7 | 7 | <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" /> |
9 | 8 | <img alt="npm version" src="https://img.shields.io/npm/v/@envato/react-breakpoints?style=for-the-badge" /> |
10 | 9 | <img alt="react version" src="https://img.shields.io/npm/dependency-version/@envato/react-breakpoints/dev/react?style=for-the-badge"> |
11 | 10 | <img alt="license" src="https://img.shields.io/npm/l/@envato/react-breakpoints?style=for-the-badge" /> |
|
21 | 20 |
|
22 | 21 | This package provides you with: |
23 | 22 |
|
24 | | -* a [`<Provider>`](/docs/api.md#provider) to instantiate the ResizeObserver; |
25 | | -* an [`<Observe>`](/docs/api.md#observe) component to observe changes in a DOM element; |
26 | | -* a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent `<Observe>`. |
| 23 | +* a [`<Provider>`](/docs/api.md#provider) to instantiate the `ResizeObserver`; |
| 24 | +* an [`<Observe>`](/docs/api.md#observe) component to observe changes in a DOM element and respond to them. |
27 | 25 |
|
28 | 26 | For power users this package also provides: |
29 | 27 |
|
30 | | -* a [`<Context>`](/docs/api.md#context) on which you can assign a `ResizeObserverEntry` value to update any nested components that are using `useBreakpoints()`; |
| 28 | +* a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent `<Observe>`; |
31 | 29 | * a [`useResizeObserver()`](/docs/api.md#useresizeobserver) hook to connect a DOM element in your component to the instantiated `ResizeObserver` on `<Provider>`; |
32 | 30 | * a [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) hook to retrieve the `ResizeObserverEntry` put on the nearest `<Context>`. This is what `useBreakpoints()` uses under the hood. |
33 | 31 |
|
@@ -59,56 +57,41 @@ const App = () => ( |
59 | 57 |
|
60 | 58 | ## Observe an element |
61 | 59 |
|
62 | | -Everything you render through `<Observe>` becomes aware of the size of the element that is given `{...observedElementProps}`. This is called an "Observe Scope". |
| 60 | +Everything you render through `<Observe>` has access to the size of the element that is given `{...observedElementProps}`. This is called the "Observe Scope". |
63 | 61 |
|
64 | 62 | ```javascript |
65 | 63 | import { Observe } from '@envato/react-breakpoints'; |
66 | 64 |
|
67 | 65 | const MyObservingComponent = () => ( |
68 | 66 | <Observe |
69 | | - render={({ observedElementProps }) => ( |
70 | | - <aside {...observedElementProps}> |
71 | | - <MyResponsiveComponent /> |
72 | | - </aside> |
| 67 | + breakpoints={{ |
| 68 | + widths: { |
| 69 | + 0: 'mobile', |
| 70 | + 769: 'tablet', |
| 71 | + 1025: 'desktop' |
| 72 | + } |
| 73 | + }} |
| 74 | + render={({ observedElementProps, widthMatch = 'ssr' }) => ( |
| 75 | + <div {...observedElementProps}> |
| 76 | + <div className={widthMatch}> |
| 77 | + </div> |
73 | 78 | )} |
74 | 79 | /> |
75 | 80 | ); |
76 | 81 | ``` |
77 | 82 |
|
78 | | -## Consume the observation |
79 | | - |
80 | | -Components that are rendered within the "Observe Scope" can consume observation results via `useBreakpoints()`: |
81 | | - |
82 | | -```javascript |
83 | | -import { useBreakpoints } from '@envato/react-breakpoints'; |
84 | | - |
85 | | -const MyResponsiveComponent = () => { |
86 | | - const options = { |
87 | | - widths: { |
88 | | - 0: 'mobile', |
89 | | - 769: 'tablet', |
90 | | - 1025: 'desktop' |
91 | | - } |
92 | | - }; |
93 | | - |
94 | | - const [label] = useBreakpoints(options); |
95 | | - |
96 | | - return ( |
97 | | - <div className={label}> |
98 | | - This element is currently within the {label} range. |
99 | | - </div> |
100 | | - ); |
101 | | -}; |
102 | | -``` |
103 | | - |
104 | | -However, `<Observe>` supports additional props allowing you to observe **and** consume observations — no `useBreakpoints()` required! |
105 | | - |
106 | 83 | See the [API Docs](/docs/api.md) for reference guides and usage examples. |
107 | 84 |
|
108 | 85 | # Observing vs. Consuming boxes |
109 | 86 |
|
110 | 87 | There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints. See [Observing vs. Consuming Boxes](/docs/boxes.md) for more information. |
111 | 88 |
|
| 89 | +# Re-rendering |
| 90 | + |
| 91 | +Using [`useResizeObserver()`](/docs/api.md#useresizeobserver), [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) or [`useBreakpoints()`](/docs/api.md#usebreakpoints) in your components causes them to re-render **every time a resize is observed**. |
| 92 | + |
| 93 | +In some cases, you may want to optimise this. If you only want to re-render your components when breakpoint values actually change, use `React.useMemo` or `React.memo`. |
| 94 | + |
112 | 95 | # Server-Side Rendering |
113 | 96 |
|
114 | 97 | See [Server-Side Rendering](/docs/server-side-rendering.md) for more information. |
|
0 commit comments