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

react-r2d2

v0.4.2

Published

HOC for dispatching an action automatically on component render

Downloads

20

Readme

npm version

R2D2

A React Higher Order Component (HOC) for ensuring your data makes it to your component in time. This was inspired to reduce the code required for the common use case of retrieving data from the Redux store, and dispatching an action to fetch it if it is non-existent.

It currently supports Redux out of the box, and any store that it can retrieve from context under the store key. Please feel welcome to contribute support for other libraries as well.

WIP: Currently in testing, during this time the API is experimental and subject to change.

Usage

Install the latest stable version:

npm i react-r2d2

We currently support CommonJS, AMD, and ES module types. If you like to live on the edge and use Rollup, feel free to use that as well.

Running the tests

To run the tests included with the package. Run the command:

npm run test

The test suit is written using Mocha for execution and Chai for expect-style assertions. It also utilizes Enzyme for DOM and React component manipulation.

API

Calling the HOC with passing in your component as the argument, you will be returned a new component which will accept the following props.

Required props

  • selector, function: This is the Redux selector method to select the portion of store you would like to validate.

  • action, { type: String, payload: Any }: The Redux action you would like to dispatch to fetch the data to populate the selected portion of the store.

Optional props

  • validateStore, function: this is an optional function that will determine the validity of the selected portion of store to fulfill the wrapped components requirements. This will default to a simple selectedState => !!selectedState boolean check.

  • altComponent, node: this optional node will be shown while the validity of the selected state is false. This could be a React component, or a simple text node. This is ideal for placing a loading component while the data is being fetched.

Usage

An example of this HOC in the wild.

Component.js
import React from 'react';
import R2D2 from 'react-r2d2';

const Component = ({ data }) => {
  return (
    <div>I will always render with {data}</div>
  );
};

export default R2D2(Component);
Container.js
import React, { Component } from 'react';

import WrappedComponent from './Component';

import { selector } from './selectors';
import { action } from './actions';

// If the store is not able to be validated, this component will be rendered as altComponent.
const Loading = () => <div>Loading...</div>;

class Container extends Component {
  state = {}
  
  render() {
    return (
      <div>
        <WrappedComponent
          selector={selector}
          action={action}
          {/* v Optional v */}
          altComponent={Loading}
          validateStore={state => state.id !== undefined}
        />
      </div>
    );
  }
}

export default Container;

Contributing

Feel free to open a PR contributing a feature addition, bugfix or even adding/improving documentation. PR's are always welcome! :)

Issues

As this project is still in Alpha, there is a very significant chance of bugs popping up. If you do run into something of the sort, please feel free to open an issue and it will be looked into ASAP.

License

MIT