• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋

English | 中文

Npm Version Package License Downloads

Overview

🐻 Clean-State is a neat, compact state management tool. It drops all of React's historical baggage, uses native hooks to implement it, and gets rid of Redux's problem of invalid rendering during status updates. At the architectural level it is automatically organized through a very simple API. 🍋 If you're not building an aircraft carrier and you're tired of having a large, complex and unwield-of-use State management library, try clean-state. It is small and exquisite, the performance of the extreme can meet your needs.

Features

  1. Using native hooks implementation, zero external dependencies.
  2. The structure is simple, the module layer granularity is fine and measurable, and the division is clear.
  3. Excellent performance, can do module level accurate update.
  4. Native support side effects.
  5. It's extremely small, just 200 lines of code.
  6. Just React syntax, zero learning access cost.
  7. TypeScript friendly and automatically deduces module types.
  8. Support for Redux - Tool debugging tool.
  9. Perfect support for RN.

Installation

npm i clean-state --save

Usage

1.Define a module

// modules/user.ts
const state = {
  name: 'test'
}

const user = {
  state,
  reducers: {
    setName({payload, state}) {
      return {...state, ...payload}
    }
  },
  effects: {
    async fetchNameAndSet({dispatch}) {
      const name = await Promise.resolve('fetch_name')
      dispatch.user.setName({name})
    }
  }
}

export default user;

2.Registration module

// modules/index.ts
import user from './user'
import bootstrap from 'clean-state'

const modules = { user }
export const {useModule, dispatch}  = bootstrap(modules);

3.Use the module

// page.ts
import {useCallback} from 'react'
import { useModule, dispatch } from './modules'

function App() {
  /** 
   * Here you can also pass in an array and return multiple module states at the same time 
   * const {user, project} = useModule(['user', 'project'])
   */
  const { user } = useModule('user')
  const onChange = useCallback((e)=> {
    const { target } = e
    dispatch.user.setName({name: target.value})
  }, [])

  const onClick = useCallback(()=> {
    dispatch.user.fetchNameAndSet()
  }, [])

  return (
    <div className="App">
      <div>
        <div>
          name: {user.name}
        </div>
        <div>
          modify: <input onChange={onChange}></input>
        </div>
        <button onClick={onClick}>getUserName</button>
      </div>
    </div>
  );
}

export default App;

Mixin

In many cases, there are common states, reducers, or effects between multiple modules, and here we expose the methods to prevent users from making duplicate declarations in each module.

// common.ts
const common = {
  reducers: {
    setValue<State>({payload, state}: {payload: Record<string, any>, state: State}): State {
      return {...state, ...payload}
    }
  }
}
export default common;

// modules/index.ts
import commont from './common'
import user from './user'
import { mixin } from 'clean-state';

// Mix Common's setValue method into the User module
const modules = mixin(common, { user })

// You can now call the dispatch.user.setValue method on other pages
export const {useModule, dispatch}  = bootstrap(modules);

Module

state

Module state, which is a property object.

{
    name: 'zhangsan',
    order: 1
}

reducers

A collection of handlers that change the state of a module, returning the latest state.

{
    setValue({payload, state, rootState}) {
        return {...state, ...payload}
    }
}

effects

Module's collection of side effects methods that handle asynchronous calls.

{
    async fetchAndSetValue({payload, state, rootState, dispatch}) {
        const newOrder = await fetch('xxx')
        dispatch.user.setValue({order: newOrder})
    }
}

API

bootstrap(modules)

Property Description Type
modules A collection of registered modules {string, Module}

useModule(moduleName)

Property Description Type
moduleName The name of the module used returns the corresponding status string / string[]

mixin(common, modules)

Property Description Type
common Public modules that need to be injected Module
modules A collection of registered modules Module

dispatch.{moduleName}.{fnName}(payload)

Property Description Type
moduleName The specific module name of the call should be registered in Bootstrap string
fnName The method name of the call module, reducer/effect string
payload The load value passed object

Debugging

You can use cs-redux-devtool to debug your project and track historical data changes.

Notice

Dispatch calls take precedence at effects-> reducers, so when there are reducers and effects with the same name under a module, only effects are executed.

Issues

If you have better suggestions for this library, or have any problems using it, you can write them here: https://github.com/tnfe/clean-state/issues

License

MIT

More Repositories

1

TNT-Weekly

🙈 🙉 🙊 每周为您推荐国内外前端领域最新的优秀文章以及行业进展
5,158
star
2

FFCreator

A fast video processing library based on node.js (一个基于node.js的高速视频制作库)
JavaScript
2,816
star
3

awesome-blackmagic

🎭 ♠♥奇技淫巧 💠黑魔法大集合♦♣ 👺
1,027
star
4

wp2vite

一个让webpack项目支持vite的前端项目的转换工具。A front-end project automatic conversion tool。
JavaScript
709
star
5

shida

《视搭》是一个视频可视化搭建项目。您可以通过简单的拖拽方式快速生产一个短视频,使用方式就像易企秀或Maka 等 h5 搭建工具一样的简单,仅抛砖引玉希望您喜欢。
Vue
569
star
6

FEDiagram

图说前端>>收集各种前端技术图谱 🚕🚖🚗🚚🚛🚜
440
star
7

csijs

CSI.JS是一个特别的前端日志系统,帮你快速重建犯罪现场。
JavaScript
275
star
8

bbo

bbo is a utility library of zero dependencies for javascript. 🍖🌭🍔
JavaScript
275
star
9

vue3-infinite-list

一个支持百万数量级的Vue3无限滚动列表组件
TypeScript
224
star
10

limu

High performance immutable lib alternative to immer with the same api, based on shallow copy on read and mark modified on write mechanism.
JavaScript
217
star
11

awesome-state

collection of state management lib
187
star
12

transx

一个小巧玲珑的 vue 组件切换动画库, 支持 20 几种动画切换方式
JavaScript
183
star
13

tntweb-admin

react admin management system template
CSS
38
star
14

awesome-ffcreator

awesome ffcreator projects
30
star
15

jscalpel

A small feature library that makes it easier to manipulate objects
JavaScript
27
star
16

manage-table

对antd的table进行扩展,支持配置展示列
JavaScript
13
star
17

data-filler

make your backend response data shape reliable, with data-filler you can stay away from optional chain
JavaScript
2
star
18

html3canvas

html convert to canvas
TypeScript
1
star
19

hel

a solution of using micro module for frontend development
TypeScript
1
star