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

state-props

v0.4.5

Published

Inject state into props (for stateless react components)

Downloads

31

Readme

State Props

Add state to stateless components through props, using declarative-style.

Usage

stateProps is a Higher-order Component that inserts state into a React component as props.state.

By default it inserts two props (props.state) and (props.setState) that are similar to this.state and this.setState in a regular React component. For example:

function Stateless ({state, setState}) {
  return (
    <div style="background-color:{state.color}; margin:20px">
      <button onClick={setState({color:"blue"})}>Blue</button>
      <button onClick={setState({color:"red"})}>Red</button>
    </button>
  );
}
const Stateful = stateProps({color:"silver"})(Stateless);

Arguments

The stateProps function accepts the following arguments (all optional):

stateProps ( initialState, mutations, mergeProps )
  • initialState is a plain object describing the initial state passed as props.state. By default, it is an empty object:

    initialState = {}
  • mutations is an object with methods. Each method is a state mutation (see below). By default, it provides the setState mutation:

    mutations = { setState: changes => state => Object.assign({},state,changes) }
  • mergeProps is a function that takes the props generated from the mutations and those passed to the component and returns the props to be passed to the wrapped component. It is called every time the component's props change. By default it simply merges them:

    mergeProps = (mutators,ownProps) => Object.assign({},mutators,ownProps)

Mutations

A mutation is a function returning the new state based on some arguments:

(...args) => nextState

If the next state needs to be based also on the current state, the mutation can also return a function that takes the current state as argument, thus, the mutation is a function returning a function:

(...args) => (state) => nextState

The outer function takes any number of arguments, and the inner function takes a state, returning the next state. For example:

const addToCounter = (increase) => (state) => ({
  counter: state.counter + increase
});

Both functions should compose as a pure function. I.e., they should not cause side effects, nor use values other than those provided as arguments to both. The arguments shall be considered immutable.

The mutations argument passed to stateProps shall be an object with each method being one of these mutations. For example:

const stateMutations = {
  set: (x) => ({counter:x}),
  increment: () => state => ({counter: state.counter+x})
}

Stateful stateless components

React 0.14 introduced a new way to define entire stateless components as simple functions.

This is an amazing feature, and a step toward implementing fully functional components. Ideally, components should have the minimum amount of state (or no state at all) and obtain as much as possible of its data input in the form of props. But, sometimes, components really need to have their own state, or it simply makes sense they own the state and not some abstract store. This package aims to bring this to stateless components, without losing their purely functional nature.

Example

function StatelessComponent ({state, increment, set}) {
  return (
    <div>
      counter: {state.counter}
      <button onClick={increment}>Increment</button>
      <button onClick={()=>set(100)}>Set to 100</button>
    </div>
  );
}
const initialState = {
  counter:0
};
const mutations = {
  set: x => state => {counter:x},
  increment: () => state => ({
    counter: state.counter + 1
  })
};
const Component = stateProps(initialState,mutations)(StatelessComponent);

The stateProps Higher-order Component keeps the current state internally and passes it to the stateless component as props.state. It also injects a mutator function in the props for every mutation passed as second argument.

Mutation vs mutator

  • A mutation is one of the methods passed to stateProps. Each mutation simply describes a state change:
    (...args) => (state) => nextState
  • A mutator is a function passed to the actual component as a prop. Calling this function actually performs the change described by the mutation with the same name. stateProps automatically generates the mutators from the mutations it is passed.

A mutation describes a change; a mutator performs that change in the component's state.

Redux

Redux uses actions and reducers to separate imperative behavior (with functions that dispatch actions) from declarative state changes (reducers that translate actions to state changes).

The declarative nature of this package's mutations, makes them a pretty similar concept to redux's reducers (both take a state and return its replacement). The advantage of considering only local state is that actions are dispatched by a component and reduced for the state of that same component, making the actions' shape trivial and its creation repetitive. By using mutations objects, we combine actions and reducers into one, reducing boilerplate while preserving the declarative nature of reducers.

There is a version of this package that stores the state of your components in a redux store. Please see (redux-state-props).

Example

See Todo-List Example

License

MIT License (c) Juan Soulie, 2016