All Projects → jxnblk → Refunk

jxnblk / Refunk

Licence: mit
🎧 Simple React functional setState

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Refunk

Tiny Atom
Pragmatic and concise state management.
Stars: ✭ 109 (-54.96%)
Mutual labels:  functional, state
Alfa
Effortless React State Management.
Stars: ✭ 86 (-64.46%)
Mutual labels:  functional, state
react-wisteria
Managing the State with the Golden Path
Stars: ✭ 18 (-92.56%)
Mutual labels:  functional, state
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+716.94%)
Mutual labels:  functional, state
Godux
State Management for Go Backend application inspired in Redux.
Stars: ✭ 222 (-8.26%)
Mutual labels:  state
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (-16.94%)
Mutual labels:  state
Koin Samples
KOIN - a concise and pragmatic dependency injection framework for Kotlin -- #Samples
Stars: ✭ 200 (-17.36%)
Mutual labels:  functional
Deep Waters
🔥Deep Waters is an easy-to-compose functional validation system for javascript developers 🔥
Stars: ✭ 188 (-22.31%)
Mutual labels:  functional
Bukubrow Webext
WebExtension for Buku
Stars: ✭ 240 (-0.83%)
Mutual labels:  functional
Getx pattern
Design pattern designed to standardize your projects with GetX on Flutter.
Stars: ✭ 225 (-7.02%)
Mutual labels:  state
Aioreactive
Async/await reactive tools for Python 3.9+
Stars: ✭ 215 (-11.16%)
Mutual labels:  functional
Fxjs
Functional Extensions Library for JavaScript
Stars: ✭ 202 (-16.53%)
Mutual labels:  functional
Kaskade
[INACTIVE] Simplifying state management
Stars: ✭ 223 (-7.85%)
Mutual labels:  state
Gluon
A static, type inferred and embeddable language written in Rust.
Stars: ✭ 2,457 (+915.29%)
Mutual labels:  functional
Dsladapter
🔥 Kotlin时代的Adapter, Dsl 的形式使用 RecyclerView.Adapter, 支持折叠展开, 树结构,悬停,情感图状态切换, 加载更多, 多类型Item,侧滑菜单等
Stars: ✭ 231 (-4.55%)
Mutual labels:  state
Swiftparsec
A parser combinator library written in the Swift programming language.
Stars: ✭ 192 (-20.66%)
Mutual labels:  functional
Falco
A functional-first toolkit for building brilliant ASP.NET Core applications using F#.
Stars: ✭ 214 (-11.57%)
Mutual labels:  functional
Laravel World
provide countries, states, and cities relations and database.
Stars: ✭ 222 (-8.26%)
Mutual labels:  state
React Easy State
Simple React state management. Made with ❤️ and ES6 Proxies.
Stars: ✭ 2,459 (+916.12%)
Mutual labels:  state
Save Page State
A chrome extension to save the state of a page for further analysis
Stars: ✭ 208 (-14.05%)
Mutual labels:  state

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].