Partial React hydration wrapper component based on screen width for faster responsive performances
npm install partial-hydrate!intro
Provides a component that conditionally _skips hydrating children_ by removing them from the DOM _before the first client render_. Removing them before ensures hydration is successful and there are no hydration mismatch errors.
```
npm i partial-hydrate
`tsx`
window.innerWidth <= 680
}}
>
{/ My mobile component /}
- minWidth: will render if window width is greater than minWidth value.maxWidth
- : will render if window width is lesser than maxWidth value.when()
- : function that must return true for the render to happen.
You can use the minWidth and/or maxWidth props individually or together to conditionally render components based on the window width. Here's an example:
`tsx
const MyComponent = () => {
return (
{ / Rendered if window width is greater than or equal to 768 pixels / }
{ / Rendered if window width is less than or equal to 1024 pixels / }
{ / Rendered if window width is between 768 and 1024 pixels (inclusive) / }
)
}
`
The when() prop allows for a custom condition based on a function. It is particularly useful for your _dynamic conditions_. For example:
`tsxwhen()
const MyComponent = () => {
return (
{/ Rendered if the custom condition specified in the function is true /}`
)
}
When using React's server-side rendering, we often need to render components on the server even if they are conditional on the client _e.g. hidden based on window width_.
In order for hydration to succeed, the first client render must match the DOM (which is generated from the HTML returned by the server), otherwise we will get hydration mismatch errors. This means the component must be rendered again during the first client render.
However, hydration is expensive, so we really don't want to pay that penalty only for the element to be hidden or removed immediately afterwards.
So is this another react _responsive_ rendering library? Nope. If the prop conditions are not met, then 's children are never rendered.
#### ✋ Keep in mind
Also, keep in mind that using
Based on a gist by OliverJAsh. Developed, modified and maintained by George Cht.
- George Cht (@GeorgeCht)
- Oliver Joseph Ash (@OliverJAsh)
[downloads-image]: http://img.shields.io/npm/dm/partial-hydrate.svg
[npm-url]: https://www.npmjs.com/package/partial-hydrate
[npm-image]: http://img.shields.io/npm/v/partial-hydrate.svg