# statty
**Repository Path**: mirrors/statty
## Basic Information
- **Project Name**: statty
- **Description**: statty 是一个小巧而又不引人注目的,用于 React 和 Preact 应用程序的状态管理库
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: master
- **Homepage**: https://www.oschina.net/p/statty
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 1
- **Created**: 2017-10-05
- **Last Updated**: 2025-09-20
## Categories & Tags
**Categories**: react-extensions
**Tags**: None
## README
# statty
> A tiny and unobtrusive state management library for React and Preact apps
[](https://travis-ci.org/vesparny/statty)
[](https://codecov.io/github/vesparny/statty)
[](https://david-dm.org/vesparny/statty)
[](https://www.npmjs.com/package/statty)
[](https://npm-stat.com/charts.html?package=statty&from=2017-05-19)
[](http://standardjs.com/)
[](https://github.com/vesparny/statty/blob/master/LICENSE)
The current size of `statty/dist/statty.umd.min.js` is:
[](https://unpkg.com/statty/dist/)
## 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](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367) 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](https://medium.com/@mweststrate/how-to-safely-use-react-context-b7e343eff076) 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](#installation)
- [Usage](#usage)
- [API](#api)
- [Examples](#examples)
- [Inspiration](#inspiration)
- [LICENSE](#license)
## Installation
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Check them out if you don't have them locally installed.
```sh
$ npm i statty
```
Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:
```javascript
// using ES6 modules
import statty from 'statty'
// using CommonJS modules
var statty = require('statty')
```
The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
```html
```
You can find the library on `window.statty`.
## Usage
```jsx
// 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 component to access the state
// and the update function used to execute state mutations
const Counter = () => (
(