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

enable-hot-reload

v2.0.0-2

Published

Library to generate make React components hot-reloadable. Compatible with `create-react-app`.

Downloads

5

Readme

enable-hot-reload

Library to generate make React components hot-reloadable. Compatible with create-react-app.

This library is a simple wrapper around webpack’s HMR API to make it easier to create hot-reloadable React components. No loader or Babel transform plugins needed. The goal of this library is to be as simple and without magic as possible (the source code is less than 200 lines).

This library should be used strategically in components where hot-reloading would be very beneficial. Don’t go overboard and enable hot reload on every component!

Installation

yarn add enable-hot-reload

Usage

First, put this into the module you want to opt in to hot module replacement:

import enableHotReload from 'enable-hot-reload'
const hot = enableHotReload(module)

Use hot(React, Component[, name]) to generate a hot-reloadable wrapper.

  • If your module exports a single component:

    class App extends React.Component { /* ... */ }
    export default hot(React, App)
  • If your module exports multiple components:

    function _Button () { /* ... */ }
    export const Button = hot(React, _Button, 'Button')
    
    function _Icon () { /* ... */ }
    export const Icon = hot(React, _Icon, 'Icon')
    
    class _Layout extends React.Component { /* ... */ }
    export const Layout = hot(React, _Layout, 'Layout')

hot(React, Component[, name]) returns a wrapper component class. Use .WrappedComponent to access the wrapped component class.

API

hot = enableHotReload(module)

Sets the module up for hot-reloading and returns the hot() API.

hot(React, Component[, name = 'default']) → ComponentWrapper

Generates a hot-reloadable wrapper for a React component.

  • React The React library.
  • Component The component to wrap.
  • name The unique name of the component. This allows enable-hot-reload to keep track of which components to update

Returns a ComponentWrapper, a React component that wraps your component with the ability to hot-reload.

When the module is updated and this function is called with a the new component, the wrapper will replace all instances of the old component with the new one.

Notes:

  • Component state is not preserved. The old component will be unmounted, and the new component will be mounted in its place.

    • Workaround: If you wish to do state-preserving hot-reloads, extract the rendering logic into a stateless presentational component, and make that component hot-reloadable instead.
  • refs are not supported. Again, this library should be used strategically. In most cases, you shouldn’t have to hot-reload a component that you need to ref it.

    • Workaround: If you insist of having a ref-able hot-reloadable component, create a hot-reloadable wrapper that passes innerRef props instead:

      class TheComponentYouNeedToRef extends React.Component { /* ... */ }
      
      export default hot(React, ({ innerRef, ...props }) => {
        return <TheComponentYouNeedToRef {...props} ref={innerRef} />
      })
  • Optimization: Stateless components will be updated in-place without remounting. Exception: if the stateless component makes use of React context, it will be remounted.

ComponentWrapper.WrappedComponent

Use this to access the wrapped component class.