partial-hydrate
TypeScript icon, indicating that this package has built-in type declarations

1.2.8 • Public • Published

Partial Hydrate npm version npm bundle size Downloads Test

Introduction

intro Provides a <PartialHydrate> 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.

Install

npm i partial-hydrate

Usage

<PartialHydrate
  when={() => {
    window.innerWidth <= 680
  }}
>
  {/* My mobile component */}
</PartialHydrate>

Props

  • 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.

Use with minWidth and/or maxWidth

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:

const MyComponent = () => {
  return (
    <PartialHydrate minWidth={768}>
      { /* Rendered if window width is greater than or equal to 768 pixels */ }
    </PartialHydrate>

    <PartialHydrate maxWidth={1024}>
      { /* Rendered if window width is less than or equal to 1024 pixels */ }
    </PartialHydrate>

    <PartialHydrate minWidth={768} maxWidth={1024}>
      { /* Rendered if window width is between 768 and 1024 pixels (inclusive) */ }
    </PartialHydrate>
  )
}

Use with when()

The when() prop allows for a custom condition based on a function. It is particularly useful for your dynamic conditions. For example:

const MyComponent = () => {
  return (
    <PartialHydrate when={() => someDynamicCondition()}>
      {/* Rendered if the custom condition specified in the `when()` function is true */}
    </PartialHydrate>
  )
}

Use case

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.

Caveats

So is this another react responsive rendering library? Nope. If the prop conditions are not met, then <PartialHydrate>'s children are never rendered.

✋ Keep in mind

Also, keep in mind that using <PartialHydrate> does not work on window resize and it is not meant to!

Authors

Based on a gist by OliverJAsh. Developed, modified and maintained by George Cht.

License

MIT License

Package Sidebar

Install

npm i partial-hydrate

Weekly Downloads

11

Version

1.2.8

License

MIT

Unpacked Size

370 kB

Total Files

22

Last publish

Collaborators

  • georgecht