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-class-container

v2.3.0

Published

Manage your react containers as es6 classes

Downloads

37

Readme

react-class-container

For when you want to seperate your components presentation and logic (for example when spliting work between designers and programmers) or you want your redux-connected container to implement some lifecycle methods without struggle.

Install

npm i react-class-container

Usage

A class container is created by defining a class that inherits from Container(...). Your class then may provide a custom implementation of getChildProps, whose return value will be passed to the wrapped component. If you dont provide a custom method implementation your container's props and state will be spread and passed to the wrapped component.

Basic example

// mycomponent.js

const MyComponent = props => <span>{props.greeting}</span>
// mycontainer.js

import { Container } from "react-class-container"

/*
* Container(MyComponent) meens 'create a class that will always render MyComponent'
*
* As it is an ordinary React Component you can implement any of it's lifecycle methods
* or assign additional methods and properties to it.
*/
class MyContainer extends Container(MyComponent) {
  /*
  * this will be available as `props.foo` inside MyComponent
  */
  getChildProps(props, state) {
    return { greeting: "foo" }
  }
}

Using state

const MyInput = props => <input {...props} />
import { Container } from "react-class-container"

/*
* Like any other class based React Component a class container
* may hold and manage it's own state (and pass is to the wrapped template component).
* 
*/
class MyInputContainer extends Container(MyComponent) {
  state = {
    value: ""
  }

  /*
  * `onChange` is implemented as arrow function property
  * so `this` is bound properly to the function
  * and its value stays the same on each call of
  * `getChildProps`.
  */
  onChange = e => this.setState({ value: e.target.value })

  getChildProps(props, state) {
    return {
      ...state,
      /*
      * Try to return a static value.
      * If you return `this.onChange.bind(this)` shallow comparison of
      * of your child props will fail because `this.method.bind(this) !== this.method.bind(this)`.
      */
      onChange: onChange
    }
  }
}

Usage with redux

Every ReduxContainer component tries to get access to a context variable called store, which should be a valid redux store, if provided. The easiest way to provide this context is to the build-in Provider component or react-redux's Provider.

The access to the store is provided through the (readonly) store property on any instance of ReduxContainer. Also, one can access the store's state directly as the third parameter (aside props and state) inside the getChildProps method.

import { ReduxContainer } from "react-class-container"

class MyContainer extends ReduxContainer(MyComponent) {
  onFireSomeAction = () => {
    /*
    * `this.store` accesses the redux store you provided via `<Provider/>` or similiar.
    */
    this.store.dispatch({ type: "some_action" })
  }

  /*
  * Here, reduxState is the same as `this.store.getState()`.
  */
  getChildProps(props, state, reduxState) {
    return {
      greeting: reduxState.greeting,
      onFireSomeAction: this.onFireSomeAction
    }
  }
}

Fine-tune when to rerender

You dont have to define when to rerender after a redux state update as an additional function. react-class-container will call getChildProps on a redux store update and compares its return value shallowly to the previously (cached) return value. Therefore your container class will only update after a redux store change if it passes a changed property to its wrapped component.