概要 • 特性 • 安装 • 指南 • API • 许可 • 赞助。
中文 • English。
下一代 React 状态库,为极致的体验而生 —— 极致的既是用户体验,也是开发者体验。
秉承函数式内核,但化繁为简:
// CountState.ts
import { construction, upon } from 'react-mug';
const { r, w } = upon<number>({
[construction]: 0,
});
export const get = r((count) => count);
export const increase = w((count, delta: number) => count + delta);透传值的读操作,进一步化简:
// CountState.ts
export const get = r();所有状态操作既可以直接调用:
const count = get();
increase(1);也可以在 React 组件结合调用:
// Count.tsx
import { useR } from 'react-mug';
import { get, increase } from './CountState';
export function Count() {
const count = useR(get);
return <button onClick={() => increase(1)}>Count: {count}</button>;
}所有操作均可以借助函数态相互复用:
// CountState.ts
export const addOne = w((count) => increase(count, 1));不必深入函数体就能看明白读写范围:
// CountState.ts
export const increase = w((count, delta: number) => ...);直接以函数态轻松测试操作:
// CountState.test.ts
import { increase } from './CountState';
describe('increase', () => {
test('adds up count and delta', () => {
expect(increase(1, 2)).toBe(3);
});
});参见下方指南。
参见下方指南。
直接以普通的异步函数定义异步操作:
// CountState.ts
export const set = w();
export const query = async () => {
const { data: count } = await fetch('/api/count').then((res) => res.json());
set(count);
};npm i react-mugApache 2.0(免费商用)。
喜欢就赞助一下吧!

