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-state-hoc

v3.0.0

Published

A React higher-order component for abstracting state away

Downloads

3,032

Readme

npm version Build Status

React state component (higher-order)

A React higher-order component for abstracting away state

Keep your components simple, testable, and composable by using higher-order components. This higher-order component will abstract state away from components, so you can keep using functional stateless components.

Installation

npm install --save react-state-hoc
# or
yarn add react-state-hoc

API

withState(initialState, mapSetStateToProps?, mergeProps?)(BaseComponent)

Wraps your BaseComponent with a stateful component, passing the state into the BaseComponent as props. By default, state will be spread into the component's props, plus the setState function is passed through. If you specify a mapSetStateToProps function, setState will not be passed through.

Two optional arguments allow you to a) define state creators, and b) customise which props are passed into the BaseComponent.

  • initialState can be either:

    • An object, to be used as the component's initial state.

      withState({ visible: true })(BaseComponent)
    • A function, which maps initial props to initial state.

      withState(props => ({ visible: props.visible }))(BaseComponent)
  • mapSetStateToProps can be either:

    • An object, containing state creators. Each state creator is a function which maps input arguments to new state. State creators are a convenient shorthand which automatically binds setState into smaller functions. Functions can also be provided as well as objects: they are supported by setState (see https://reactjs.org/docs/react-component.html#setstate)

      withState(
          { visible: true },
          { setVisibility: visible => ({ visible }) }
      )(BaseComponent)
    • A function, mapping initialProps and setState to state creators.

      withState({ state: null }, initialProps => setState => ({
          setValue: value => setState({
              someState: initialProps.mapValue(value)
          })
      }))(BaseComponent)

      Default:

      () => setState => ({ setState })
            
  • mergeProps: A function mapping the current props, state and stateCreators into the BaseComponent's props.

    withState(
        { visible: true },
        { setVisibility: visible => ({ visible }) },
        (state, stateCreators, props) => ({
            ...state,
            ...stateCreators
            // deliberately not passing props through to BaseComponent
        })
    )(BaseComponent)

    Default:

    (props, state, stateCreators) => ({ ...props, ...state, ...stateCreators })

Example

import React from 'react'
import ReactDOM from 'react-dom'
import withState from 'react-state-hoc'

function StatelessButton({ counter, setCounter }) {
    return (
        <button onClick={() => setCounter(counter + 1)}>
            Clicked {counter} times.
        </button>
    )
}

const mapSetStateToProps = () => setState => ({
    setCounter: counter => setState({ counter })
})

const StatefulButton1 = withState({ counter: 0 }, mapSetStateToProps)(
    StatelessButton
)

const StatefulButton2 = withState({ counter: 10 }, mapSetStateToProps)(
    StatelessButton
)

ReactDOM.render(
    <div>
        <StatefulButton1 />
        <StatefulButton2 />
    </div>,
    document.getElementById('app')
)