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

refunk

v3.0.1

Published

Simple functional setState for React

Downloads

221

Readme

Refunk 🎧

Simple React functional setState with the new React context API (requires React v16.3 or later)

npm i refunk

Getting Started

import React from 'react'
import { connect } from 'refunk'

// Create a state provider component
const App = connect(props => (
  <div>
    <h1>count: {props.count}</h1>
    <Controls />
  </div>
))

// Updaters are functions that return state
const dec = state => ({ count: state.count - 1 })
const inc = state => ({ count: state.count + 1 })

// Connect the Controls component to the App state
const Controls = connect(props => (
  <div>
    <samp>{props.count}</samp>
    <button onClick={e => props.update(dec)}>
      -
    </button>
    <button onClick={e => props.update(inc)}>
      +
    </button>
  </div>
))

const initialState = {
  count: 0
}

// initialize state with props
render(<App {...initialState} />)

Usage

Refunk components initialize state from props and provide an update function to their consumers. When nesting Refunk components, the top-most component will control state for any child Refunk components.

The update function works the same as setState, but it's intended to be used with separate updater functions, that can be shared across many parts of an application.

connect

The connect higher-order component creates state based on props for top-level components or connects into a parent Refunk component's state when nested. This allows for the creation of stateful components that can work standalone or listen to a parent's state.

import React from 'react'
import { connect } from 'refunk'

const App = connect(props => (
  <div>
    <samp>{props.count}</samp>
  </div>
))

App.defaultProps = {
  count: 0
}

export default App

Provider

For lower-level access to React's context API, the Provider component can be used to create a context. The Refunk Provider will convert props to initial state and provide the state and update function through context.

import React from 'react'
import { Provider } from 'refunk'

const App = props => (
  <Provider count={0}>
    <div />
  </Provider>
)

Consumer

The context Consumer is also exported for lower-level access to the context API.

import React from 'react'
import { Provider, Consumer } from 'refunk'

const inc = state => ({ count: state.count + 1 })

const App = props => (
  <Provider count={0}>
    <Consumer>
      {state => (
        <React.Fragment>
          <samp>{state.count}</samp>
          <button onClick={e => state.update(inc)}>+</button>
        </React.Fragment>
      )}
    </Consumer>
  </Provider>
)

Using Updaters

Updaters are functions that are passed to the props.update() function. An updater function takes state as its only argument and returns a new state.

// updaters.js
// Create an `updaters` module with functions to update the state of the app
export const decrement = state => ({ count: state.count - 1 })
export const increment = state => ({ count: state.count + 1 })
// Counter.js
// Use the updater functions in the connected Counter component
import React from 'react'
import connect from 'refunk'
import { decrement, increment } from './updaters'

const Counter = props => (
  <div>
    <samp>Count: {props.count}</samp>
    <button onClick={e => props.update(decrement)}>
      Decrement
    </button>
    <button onClick={e => props.update(increment)}>
      Increment
    </button>
  </div>
)

export default connect(Counter)
// App.js
// Include the Counter component in App
import React from 'react'
import connect from 'refunk'
import Counter from './Counter'

const App = props => (
  <div>
    <h1>Hello</h1>
    <Counter />
  </div>
)

export default connect(App)

Build Your Own

Refunk's source is only about 50 LOC and relies on built-in React functionality. This library is intended to be used directly as a package and also to serve as an example of some ways to handle state in a React application. Feel free to fork or steal ideas from this project, and build your own version.

Concepts

Refunk is meant as a simpler, smaller alternative to other state managment libraries that makes use of React's built-in component state. Refunk uses higher-order components, the new context API, and React component state management along with functional setState to help promote the separation of presentational and container components, and to keep state updating logic outside of the components themselves.

This library also promotes keeping application state in a single location, similar to other Flux libraries and Redux.

Related


Made by Jxnblk | MIT License