@braine/statemanagement 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -0
- package/dist/index.cjs +1706 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +1683 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# EasyState
|
|
2
|
+
|
|
3
|
+
**The "State-First" Reactive Library for React.**
|
|
4
|
+
> Better performance than Context. Simpler than Redux. More integrated than TanStack Query.
|
|
5
|
+
|
|
6
|
+
[](https://badge.fury.io/js/statemanagement)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- 🚀 **Zero Boilerplate**: Just assign values. `store.count = 1`.
|
|
12
|
+
- âš¡ **Deep Reactivity**: Only re-renders components that *read* the changed property. (No `useSelector` needed).
|
|
13
|
+
- 🔄 **State-First Async**: Assign promises to state. `store.user = fetch('/user')`.
|
|
14
|
+
- Auto-suspense support.
|
|
15
|
+
- Auto-error handling.
|
|
16
|
+
- 🧠**Computed Properties**: Memoized derived state.
|
|
17
|
+
- 🛠**DevTools**: Redux DevTools integration built-in.
|
|
18
|
+
- 📦 **Tiny**: < 1kb minified & gzipped.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install statemanagement
|
|
24
|
+
# or
|
|
25
|
+
yarn add statemanagement
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### 1. Create a Store
|
|
31
|
+
```typescript
|
|
32
|
+
// store.ts
|
|
33
|
+
import { createState } from 'statemanagement';
|
|
34
|
+
|
|
35
|
+
export const store = createState({
|
|
36
|
+
count: 0,
|
|
37
|
+
increment() {
|
|
38
|
+
this.count++;
|
|
39
|
+
},
|
|
40
|
+
// Async? Just use async/await!
|
|
41
|
+
async login() {
|
|
42
|
+
this.user = await fetch('/api/login').then(r => r.json());
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Use in React
|
|
48
|
+
```tsx
|
|
49
|
+
import { useStore } from 'statemanagement';
|
|
50
|
+
import { store } from './store';
|
|
51
|
+
|
|
52
|
+
function Counter() {
|
|
53
|
+
const snap = useStore(store);
|
|
54
|
+
return <button onClick={() => store.increment()}>{snap.count}</button>;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## The "Killer Feature": Async State
|
|
59
|
+
Stop writing `isLoading`, `isError` manually. Stop creating 5 files for one API call.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const store = createState({
|
|
63
|
+
// 1. Assign a promise
|
|
64
|
+
todos: fetch('/api/todos').then(r => r.json()),
|
|
65
|
+
|
|
66
|
+
async refresh() {
|
|
67
|
+
// 2. Re-assign to refetch. UI updates automatically.
|
|
68
|
+
this.todos = fetch('/api/todos').then(r => r.json());
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
In your component:
|
|
73
|
+
```tsx
|
|
74
|
+
function TodoList() {
|
|
75
|
+
const snap = useStore(store);
|
|
76
|
+
// accessing snap.todos AUTOMATICALLY triggers Suspense if pending!
|
|
77
|
+
return <div>{snap.todos.map(t => <div key={t.id}>{t.text}</div>)}</div>;
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Advanced
|
|
82
|
+
|
|
83
|
+
### Computed Properties
|
|
84
|
+
```typescript
|
|
85
|
+
import { computed } from 'statemanagement';
|
|
86
|
+
|
|
87
|
+
const double = computed(() => store.count * 2);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Redux DevTools
|
|
91
|
+
```typescript
|
|
92
|
+
import { enableDevTools } from 'statemanagement';
|
|
93
|
+
enableDevTools(store, 'MyGlobalStore');
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Comparison
|
|
97
|
+
|
|
98
|
+
| Feature | Redux / RTK | Zustand | EasyState |
|
|
99
|
+
| :--- | :--- | :--- | :--- |
|
|
100
|
+
| **API Style** | Reducers/Actions | Hooks/Setters | Mutable (Proxies) |
|
|
101
|
+
| **Boilerplate** | High | Low | **Zero** |
|
|
102
|
+
| **Async** | Middleware / Thunks | Manual Loading State | **Native Promise Support** |
|
|
103
|
+
| **Reactivity** | Manual Selectors | Manual Selectors | **Automatic** |
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
MIT
|