Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meixg/san-immer
https://github.com/meixg/san-immer
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/meixg/san-immer
- Owner: meixg
- Created: 2021-09-16T02:28:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-16T06:16:32.000Z (over 3 years ago)
- Last Synced: 2024-05-01T17:13:16.380Z (9 months ago)
- Language: JavaScript
- Size: 92.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# san-immer
与 [san-update](https://github.com/baidu/san-update) 功能类似,用于在 [san-store](https://github.com/baidu/san-store) 中以不可变的形式修改数据。
内部是对 [immer](https://github.com/immerjs/immer) 的封装,好处是:
1. 操作简单,就像直接对数据进行可变修改一样,不需要记 .set 等 API。
2. 使用 TypeScript 时,state 有类型提示。## 使用方式
```typescript
import { store } from 'san-store'
import { produce } from 'san-immer'const baseState = {
arr: [
{
title: "Learn TypeScript",
done: true
},
{
title: "Try Immer",
done: false
}
]
}store.addAction('change', () => {
return produce(state => {
state.arr[1].done = true
state.arr.push({title: "Tweet about it"})
})
})// 对比
store.addAction('change', () => {
return builder()
.set('arr[1].done', true)
.push('arr', {title: "Tweet about it"});
})
```## next
激进一些的话,可以直接 hook `store.addAction`。使得我们可以直接这样的写:
```javascript
store.addAction('change', (payload, state, dispatch) => {
state.xxx = payload
})
```