0.5.0 • Published 3 years ago
test-imook v0.5.0
imook
基于immer 和react hooks的轻量的局部状态管理,类比custom hook,imook强调基于功能设计store。
✨特性
- 局部状态管理,精准定位store作用区间
- 无this, 状态不可变
- 支持异步action,修改状态简单化
- 支持读写分离和定制更新(衍生数据)
- 支持中断更新和批量更新同步逻辑
📦 安装
npm i co-store
yarn add co-store
⚡快速开始
1. 定义一个local store
import React from "react"
import { createLocalStore, ActUtil } from "imook";
// 创建counterStore
const initialState: number = 0
type CounterActUtil = ActUtil<number>;
export default createLocalStore(initialState, {
inc({ commit }: CounterActUtil, step = 1) {
commit((draftStore) => {
draftStore.state += step;
});
},
});
2. 使用和修改状态
import counterSotre from "./counterStore"
function Counter() {
// 返回[state, actions]
const [count, { inc }] = counterStore.useStore()
return (
<div>
count:{count}
<button onClick={inc}>+</button>
</div>
)
}
export default function Counter() {
return (
<counterStore.Provider>
<Counter />
</counterStore.Provider>
)
}