# refunk

> Simple functional setState for React

Latest version **3.0.1** (published 2018-04-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install refunk
pnpm add refunk
yarn add refunk
bun add refunk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2018-04-14 |
| First published | 2017-07-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 22.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 237 |
| Author | Brent Jackson |
| Maintainers | jxnblk |
| Keywords | react, state, functional, context, render-props, hoc |

## Links

- npm: https://www.npmjs.com/package/refunk
- Repository: https://github.com/jxnblk/refunk
- Homepage: https://github.com/jxnblk/refunk#readme
- Issues: https://github.com/jxnblk/refunk/issues
- npm.io page: https://npm.io/package/refunk

## Dependencies (1)

- [prop-types](https://npm.io/package/prop-types.md) ^15.5.10

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2018-04-14
- 2.2.3-0 (next) — 2017-12-31
- 3.0.0 — 2018-04-12
- 2.2.4 — 2018-01-12
- 2.2.3 — 2017-12-31
- 2.2.2 — 2017-12-31
- 2.2.1 — 2017-12-29
- 2.2.0 — 2017-12-29
- 2.1.0 — 2017-12-15
- 2.0.0 — 2017-10-24
- 1.0.0 — 2017-10-24
- 2.0.0-1 — 2017-09-15
- 2.0.0-0 — 2017-09-15
- 1.0.0-2 — 2017-07-16

## README

# Refunk 🎧

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


```sh
npm i refunk
```

## Getting Started

```jsx
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](#using-updaters),
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.

```jsx
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.

```jsx
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.

```jsx
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.

```jsx
// 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 })
```

```jsx
// 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)
```

```jsx
// 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](src) 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][context], and React component state management along with
[functional setState][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][flux] libraries and [Redux][redux].


### Related

- [microstate](https://github.com/estrattonbailey/microstate)
- [statty](https://github.com/vesparny/statty)
- [unistore](https://github.com/developit/unistore)
- [redux][redux]
- [unstated](https://github.com/jamiebuilds/unstated)

[context]: https://reactjs.org/docs/context.html
[setState]: https://facebook.github.io/react/docs/react-component.html#setstate
[flux]: http://facebook.github.io/flux/
[redux]: http://redux.js.org/

---

[Made by Jxnblk](http://jxnblk.com) | [MIT License](LICENSE.md)

---
_Source: https://npm.io/package/refunk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
