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-stateful-fn

v0.1.0

Published

Stateful functional components for React. Because functional setState is your friend.

Downloads

5

Readme

react-stateful-fn - Functional stateful components

Build Status NPM Version JavaScript Style Guide

  • [x] setState on functional components
  • [x] No need to bind your component methods
  • [x] Optimized for performance
  • [x] Zero dependencies
  • [x] Extremely small: < 3kB

Installation

npm install react-stateful-fn
# or
yarn add react-stateful-fn

Usage

Let's have a look at everyone's favorite sample code: A simple counter widget.

import stateful from 'react-stateful-fn'

const increase = () => state => ({ clicks: state.clicks + 1 })

const Counter = (props, state, { setState }) => (
  <div>
    <div>Clicked {state.clicks} times</div>
    <button onClick={() => setState(increase())}>Increase +</button>
  </div>
)

export default stateful(Counter, { clicks: 0 })

As you can see, functional stateful components are good friends with functional setState.

We can do better

But if you care about performance you will probably not be completely happy with the previous example:

The button's onClick handler is an arrow function defined in the functional component. Thus it will be a different function on every render and will cause the button to re-render every time.

Let's fix that:

import stateful from 'react-stateful-fn'

const increase = () => state => ({ clicks: state.clicks + 1 })

const Counter = (props, state) => (
  <div>
    <div>Clicked {state.clicks} times</div>
    <button onClick={props.onClick}>Increase +</button>
  </div>
)

export default stateful(Counter, { clicks: 0 }, {
  onClick: event => ({ setState }) => setState(increase())
})

We can wire props and setState together outside the function, similar to Redux' connect(). The handlers defined there will be passed to the component as props.

If such a handler returns a function (as seen above) then this function is called with the { setState } object, so you can update the state according to the event.

Forms

Forms can be quite an elaborate business in React. Fortunately, stateful functional components turn out to be a convenient approach for easy form implementation.

import stateful from 'react-stateful-fn'

/**
 * Use as:
 *
 * <LoginForm onLogin={(email, password) => { ... }} />
 */
const LoginForm = (props, state) => (
  <form>
    <input type='email' placeholder='Email' value={state.email} onChange={props.onEmailChange} />
    <input type='password' placeholder='Password' value={state.password} onChange={props.onPasswordChange} />
    <button type='submit' onClick={props.onSubmitClick}>Login</button>
  </form>
)

const initialState = {
  email: '',
  password: ''
}

export default stateful(LoginForm, initialState, {
  onEmailChange: event => ({ setState }) => setState({ email: event.target.value }),
  onPasswordChange: event => ({ setState }) => setState({ password: event.target.value }),
  onSubmitClick: event => (_, props, state) => props.onLogin(state.email, state.password)
})

API

stateful(component: Function, initialState: ?Object, propHandlers: ?Object): Function

Make a functional stateless component stateful.

component

This is supposed to be a functional React component. You can use any ordinary functional component. The difference to a stateless component are the additional parameters.

The component will be called with the parameters (props: Object, state: Object, { setState: Function }).

initialState (optional)

Pass a custom initial state here. Otherwise it will default to {}.

propHandlers (optional)

Use propHandlers to pass stateful event handlers as props to the component. Useful to avoid arrow function event handlers which are considered bad practice.

If a property of propHandlers is not a function it will be added to the component's props as it is.

If a propHandlers handler returns a function this function will be called with the parameters ({ setState }, props, state).

License

MIT