npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

partial-hydrate

v1.2.8

Published

Partial React hydration wrapper component based on screen width for faster responsive performances

Downloads

26

Readme

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