Giter Club home page Giter Club logo

partial-hydrate's Introduction

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

partial-hydrate's People

Contributors

primeadvocate000 avatar georgecht avatar renovate[bot] avatar

Stargazers

vlad avatar Ilya Medvedev avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.