项目
在 GitHub 查看
nanxiaobei/resso
关于
🪢 最简单的 React 状态管理器
网站与应用开发可以做什么
- Extremely simple
- Extremely smart
- Extremely small
实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.zh-CN.md
原文Link in bio to widgets, your online home screen. ➫ 🔗 kee.so
[!NOTE] 自 v0.29.0 Breaking Changes:
- (为了更好地适配 AI 与 React Compiler)在 React 组件中,始终使用
const { count } = store.useStore()获取 state。直接访问store.count或const { count } = store现用于纯数据读取。- 仅支持
react >= 18。
介绍
特性
- 极其简单
- 极其聪明
- 极其小巧
示例
安装
bun add resso
#
pnpm add resso
#
yarn add resso
#
npm i resso
使用
import resso from 'resso';
const store = resso({
count: 0,
text: 'hello',
inc() {
store.count += 1;
},
});
function App() {
const { count, inc } = store.useStore();
return (
<>
{count}
<button onClick={inc}>+</button>
<button onClick={() => (store.count -= 1)}>-</button>
</>
);
}
API
Get
// 获取 state,在组件中
const { count } = store.useStore();
// 读取 state,在方法中
function doSomething() {
const total = store.count * 2;
const filtered = store.list.filter((item) => item.name !== '');
// ...
}
Set(触发 re-render)
// 单个
store.count = 60;
store('count', (c) => c + 1);
// 多个
store({
count: 60,
text: 'world',
});
store((s) => ({
count: s.count + 1,
text: s.text === 'hello' ? 'world' : 'hello',
}));
共享 Refs
事实上它与 resso 无关,只是 JavaScript。你可以这样做:
// store.js
export const refs = {
total: 0,
};
// App.js
import store, { refs } from './store';
function App() {
refs.total = 100;
return <div />;
}
按需 Re-render
// 无 `text` 更新,不 re-render
function Text() {
const { text } = store.useStore();
return <p>{text}</p>;
}
// 仅 `count` 更新触发 re-render
function Count() {
const { count } = store.useStore();
return <p>{count}</p>;
}
// UI 中无 state,不 re-render
function Control() {
return (
<>
<button onClick={store.inc}>+</button>
<button onClick={() => (store.count -= 1)}>-</button>
</>
);
}
协议
探索相关项目
加载中…