All Projects → vesparny → Statty

vesparny / Statty

Licence: mit
A tiny and unobtrusive state management library for React and Preact apps

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Statty

teaful
🍵 Tiny, easy and powerful React state management
Stars: ✭ 638 (+23.64%)
Mutual labels:  preact, management, state
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+283.14%)
Mutual labels:  state, preact
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+224.81%)
Mutual labels:  state, preact
Stencil Store
Store is a lightweight shared state library by the StencilJS core team. Implements a simple key/value map that efficiently re-renders components when necessary.
Stars: ✭ 107 (-79.26%)
Mutual labels:  management, state
Tiny Atom
Pragmatic and concise state management.
Stars: ✭ 109 (-78.88%)
Mutual labels:  state, preact
Unstated
State so simple, it goes without saying
Stars: ✭ 7,785 (+1408.72%)
Mutual labels:  management, state
Outstated
Simple hooks-based state management for React
Stars: ✭ 102 (-80.23%)
Mutual labels:  management, state
Store
A beautifully-simple framework-agnostic modern state management library.
Stars: ✭ 204 (-60.47%)
Mutual labels:  management, state
hyperapp-example
hyperapp example
Stars: ✭ 14 (-97.29%)
Mutual labels:  preact, state
snap-state
State management in a snap 👌
Stars: ✭ 23 (-95.54%)
Mutual labels:  preact, state
Unistore
🌶 350b / 650b state container with component actions for Preact & React
Stars: ✭ 2,850 (+452.33%)
Mutual labels:  state, preact
stoxy
Stoxy is a state management API for all modern Web Technologies
Stars: ✭ 73 (-85.85%)
Mutual labels:  preact, state
Jotai
👻 Primitive and flexible state management for React
Stars: ✭ 6,453 (+1150.58%)
Mutual labels:  management, state
Create React Library
⚡CLI for creating reusable react libraries.
Stars: ✭ 4,554 (+782.56%)
Mutual labels:  preact
Robotics Rl Srl
S-RL Toolbox: Reinforcement Learning (RL) and State Representation Learning (SRL) for Robotics
Stars: ✭ 453 (-12.21%)
Mutual labels:  state
Tlroadmap
Тимлид – это ❄️, потому что в каждой компании он уникален и неповторим.
Stars: ✭ 4,398 (+752.33%)
Mutual labels:  management
Happypandax
A cross-platform server and client application for managing and reading manga and doujinshi
Stars: ✭ 432 (-16.28%)
Mutual labels:  management
Almin
Client-side DDD/CQRS for JavaScript.
Stars: ✭ 477 (-7.56%)
Mutual labels:  state
Ijk
Transforms arrays into virtual dom trees; a terse alternative to JSX and h
Stars: ✭ 452 (-12.4%)
Mutual labels:  preact
Styled Breakpoints
Simple and powerful tool for creating breakpoints in styled components and emotion. 💅
Stars: ✭ 428 (-17.05%)
Mutual labels:  preact

statty

A tiny and unobtrusive state management library for React and Preact apps

Travis Code Coverage David npm npm JavaScript Style Guide MIT License

The current size of statty/dist/statty.umd.min.js is:

gzip size

The problem

Most of the time, I see colleagues starting React projects setting up Redux + a bunch of middlewares and store enhancers by default, regardless of the project nature.

Despite Redux being awesome, it's not always needed and it may slow down the process of onboarding new developers, especially if they are new to the React ecosystem (I have often seen colleagues being stuck for hours trying to understand what was the proper way to submit a simple form).

React already comes with a built-in state management mechanism, setState(). Local component state is just fine in most of the cases.

In real world apps we often have app state, and sometimes it becomes annoying to pass it down the entire component tree, along with callbacks to update it, via props.

This solution

statty is meant to manage app-wide state and can be thought of as a simplified version of Redux.

It safely leverages context to expose application state to children, along with a function to update it when needed.

The update function acts like Redux dispatch, but instead of an action, it takes an updater function as a parameter that returns the new state.

This way it's easy to write testable updaters and to organize them as you prefer, without having to write boilerplate code.

Table of Contents

Installation

This project uses node and npm. Check them out if you don't have them locally installed.

$ npm i statty

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import statty from 'statty'

// using CommonJS modules
var statty = require('statty')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/statty/dist/statty.umd.js"></script>

You can find the library on window.statty.

Usage

// https://codesandbox.io/s/rzpxx0w34
import React from 'react'
import { render } from 'react-dom'
import { Provider, State } from 'statty'
import inspect from 'statty/inspect'

// selector is a function that returns a slice of the state
// if not specified it defaults to f => f
const selector = state => ({ count: state.count })

// updaters

// updaters MUST be pure and return a complete new state,
// like Redux reducers
const onDecrement = state => 
  Object.assign({}, state, { count: state.count - 1 })

const onIncrement = state =>
  Object.assign({}, state, { count: state.count + 1 })

// Counter uses a <State> component to access the state
// and the update function used to execute state mutations
const Counter = () => (
  <State
    select={selector}
    render={({ count }, update) => (
      <div>
        <span>Clicked: {count} times </span>
        <button onClick={() => update(onIncrement)}>+</button>{' '}
        <button onClick={() => update(onDecrement)}>-</button>{' '}
      </div>
    )}
  />
)

// initial state
const initialState = {
  count: 0
}

// The <Provider> component is supposed to be placed at the top
// of your application. It accepts the initial state and an inspect function
// useful to log state mutatations during development
// (check your dev tools to see it in action)
const App = () => (
  <Provider state={initialState} inspect={inspect}>
    <Counter />
  </Provider>
)

render(<App />, document.getElementById('root'))


The <Provider> component is used to share the state via context. The <State> component takes 2 props:

  • select is a function that takes the entire state, and returns only the part of it that the children will need
  • render is a render prop that takes the selected state and the update function as parameters, giving the user full control on what to render based on props and state.

State updates happen via special updater functions that take the old state as a parameter and return the new state, triggering a rerender.

An updater function may return the slice of the state that changed or an entire new state. In the first case the new slice will be shallowly merged with old state.

API

<Provider>

Makes state available to children <State>

props

state

object | required

The initial state

inspect

function(oldState: object, newState: object, updaterFn: function)

Use the inspect prop during development to track state changes.

statty comes with a default logger inspired by redux-logger.

<Provider
  state={{count: 0}}
  inspect={require('statty/inspect')}
/>

<State>

Connects children to state changes, and provides them with the update function

props

select

function(state: object) | defaults to s => s | returns object

Selects the slice of the state needed by the children components.

render

function(state: object, update: function) | required

A render prop that takes the state returned by the selector and an update function.

Examples

Examples exist on codesandbox.io:

If you would like to add an example, follow these steps:

  1. Fork this codesandbox
  2. Make sure your version (under dependencies) is the latest available version
  3. Update the title and description
  4. Update the code for your example (add some form of documentation to explain what it is)
  5. Add the tag: statty:example

Inspiration

Tests

$ npm run test

LICENSE

MIT License © Alessandro Arnodo

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].