• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

statty: statty 是一个小巧而又不引人注目的,用于 React 和 Preact 应用程序的状态管 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

statty

开源软件地址:

https://gitee.com/mirrors/statty

开源软件介绍:

statty

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

TravisCode CoverageDavidnpmnpmJavaScript Style GuideMIT 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 modulesimport statty from 'statty'// using CommonJS modulesvar 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/rzpxx0w34import 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 => fconst selector = state => ({ count: state.count })// updaters// updaters MUST be pure and return a complete new state,// like Redux reducersconst 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 mutationsconst 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 stateconst 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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
masks-react: Time Chat发布时间:2022-02-14
下一篇:
react-respond: 这是一个基于reactjs的布局组件发布时间:2022-02-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap