Socket
Socket
Sign inDemoInstall

react-progressive-enhancement

Package Overview
Dependencies
6
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-progressive-enhancement

React Context that progressively enhances components


Version published
Weekly downloads
18
increased by50%
Maintainers
2
Install size
1.63 MB
Created
Weekly downloads
 

Readme

Source

react-progressive-enhancement

A handy collection of HOCs for universally rendered apps 🤩

npm Build Status

TL;DR In universally rendered React apps, it is common to branch data-fetching and component-rendering depending on the environment (server or client), and defer rendering (a.k.a. "progressively enhance") some components. However, we must ensure the first client render matches the server render.

This module achieves all the above by tracking whether or not the render mode is "enhanced" with an isEnhanced boolean (true only after first client render, otherwise false), which is accessed through a withIsEnhanced HOC. Additionally a progressivelyEnhanced HOC is provided which only renders the composed component for enhanced renders.

For more info, check out this blog post.

Features

  • No dependencies (other than React ^16.3)
  • Written in TypeScript (type-annotated)
  • Easily extensible through the exported React Context's Consumer & Provider

Install

yarn add react-progressive-enhancement
# OR
npm install react-progressive-enhancement

Usage

  • Root.jsx:
import { enableProgressiveEnhancementsOnMount } from 'react-progressive-enhancement';

const Root = () => (
  <div>
    <PhotoRoute />
  </div>
);

export default enableProgressiveEnhancementsOnMount(Root);
  • PhotoRoute.jsx:
import { withIsEnhanced, progressivelyEnhance } from 'react-progressive-enhancement';

const ProgressivelyEnhancedRelatedContent = progressivelyEnhance(RelatedContent);

class PhotoRoute extends React.Component {
  componentDidMount() {
    const hasDataFromServer = !this.props.isEnhanced;

    if (!hasDataFromServer) {
      this.getPhotoRouteData();
    } else {
      // do nothing, because the server already fetched the data and passed it to the client.
    }
  }

  render() {
    return (
      <div>
        <Photo />
        {/* This component will only render after the first client render */}
        <ProgressivelyEnhancedRelatedContent />
      </div>
    );
  }
}

export default withIsEnhanced(PhotoRoute);

API Reference

enableProgressiveEnhancementsOnMount
(ComposedComponent: React.Component) => React.Component

An HOC that wraps ComposedComponent with the Context Provider. ComposedComponent should be the root-most Component in your React app.

withIsEnhanced
(ComposedComponent: React.Component) => React.Component

An HOC that provides the isEnhanced prop to ComposedComponent.

progressivelyEnhance
(ComposedComponent: React.Component) => React.Component

An HOC that defers rendering ComposedComponent until after the first client render.

Consumer, Provider
React.Context

The Context's Consumer and Provider are exported as well, so that you can easily extend this library as you see fit.

FAQs

Last updated on 29 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc