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


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.


npm i partial-hydrate


  when={() => {
    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.

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 maxWidth={1024}>
      { /* Rendered if window width is less than or equal to 1024 pixels */ }

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

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 */}

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.


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!


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


MIT License

Package Sidebar


npm i partial-hydrate

Weekly Downloads






Unpacked Size

370 kB

Total Files


Last publish


  • georgecht