muya 2.0.0 → 2.0.1
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 +9 -11
- package/package.json +1 -1
- package/src/types.ts +1 -1
- package/types/types.d.ts +1 -1
package/README.md
CHANGED
|
@@ -9,10 +9,6 @@ Muya is simple and lightweight react state management library.
|
|
|
9
9
|
[](https://github.com/samuelgja/muya/actions/workflows/code-check.yml)
|
|
10
10
|
[](https://bundlephobia.com/result?p=muya)
|
|
11
11
|
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
## 🚀 **Key Features**
|
|
16
12
|
|
|
17
13
|
- **Simplified API**: Only `create` and `select`.
|
|
18
14
|
- **Batch Updates**: Built-in batching ensures efficient `muya` state updates internally.
|
|
@@ -43,14 +39,14 @@ yarn add muya@latest
|
|
|
43
39
|
```typescript
|
|
44
40
|
import { create } from 'muya';
|
|
45
41
|
|
|
46
|
-
const
|
|
42
|
+
const useCounter = create(0);
|
|
47
43
|
|
|
48
44
|
// Access in a React component
|
|
49
45
|
function Counter() {
|
|
50
|
-
const count =
|
|
46
|
+
const count = useCounter(); // Call state directly
|
|
51
47
|
return (
|
|
52
48
|
<div>
|
|
53
|
-
<button onClick={() =>
|
|
49
|
+
<button onClick={() => useCounter.set((prev) => prev + 1)}>Increment</button>
|
|
54
50
|
<p>Count: {count}</p>
|
|
55
51
|
</div>
|
|
56
52
|
);
|
|
@@ -80,9 +76,11 @@ const asyncCountSlice = state.select(async (s) => {
|
|
|
80
76
|
|
|
81
77
|
### **Combine Multiple States**
|
|
82
78
|
|
|
83
|
-
Combine multiple states into a derived state:
|
|
79
|
+
Combine multiple states into a derived state via `select` method:
|
|
84
80
|
|
|
85
81
|
```typescript
|
|
82
|
+
import { create, select } from 'muya'
|
|
83
|
+
|
|
86
84
|
const state1 = create(1);
|
|
87
85
|
const state2 = create(2);
|
|
88
86
|
|
|
@@ -114,7 +112,7 @@ const derived = select([state1, state2], (s1, s2) => s1 + s2, (prev, next) => pr
|
|
|
114
112
|
Access state directly or through `useValue` hook:
|
|
115
113
|
|
|
116
114
|
### **Option 1: Access State Directly**
|
|
117
|
-
|
|
115
|
+
Each state can be called as the hook directly
|
|
118
116
|
```typescript
|
|
119
117
|
const userState = create(0);
|
|
120
118
|
|
|
@@ -125,7 +123,7 @@ function App() {
|
|
|
125
123
|
```
|
|
126
124
|
|
|
127
125
|
### **Option 2: Use the Hook**
|
|
128
|
-
|
|
126
|
+
Or for convenience, there is `useValue` method
|
|
129
127
|
```typescript
|
|
130
128
|
import { useValue } from 'muya';
|
|
131
129
|
|
|
@@ -136,7 +134,7 @@ function App() {
|
|
|
136
134
|
```
|
|
137
135
|
|
|
138
136
|
### **Option 3: Slice with Hook**
|
|
139
|
-
|
|
137
|
+
For efficient re-renders, `useValue` provides a slicing method.
|
|
140
138
|
```typescript
|
|
141
139
|
function App() {
|
|
142
140
|
const count = useValue(state, (s) => s.count); // Use selector in hook
|
package/package.json
CHANGED
package/src/types.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type IsEqual<T = unknown> = (a: T, b: T) => boolean
|
|
|
4
4
|
export type SetStateCb<T> = (value: T | Awaited<T>) => Awaited<T>
|
|
5
5
|
export type SetValue<T> = SetStateCb<T> | Awaited<T>
|
|
6
6
|
export type DefaultValue<T> = T | (() => T)
|
|
7
|
-
export type Listener<T> = (listener: (value
|
|
7
|
+
export type Listener<T> = (listener: (value: T) => void) => () => void
|
|
8
8
|
export interface Cache<T> {
|
|
9
9
|
current?: T
|
|
10
10
|
previous?: T
|
package/types/types.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type IsEqual<T = unknown> = (a: T, b: T) => boolean;
|
|
|
3
3
|
export type SetStateCb<T> = (value: T | Awaited<T>) => Awaited<T>;
|
|
4
4
|
export type SetValue<T> = SetStateCb<T> | Awaited<T>;
|
|
5
5
|
export type DefaultValue<T> = T | (() => T);
|
|
6
|
-
export type Listener<T> = (listener: (value
|
|
6
|
+
export type Listener<T> = (listener: (value: T) => void) => () => void;
|
|
7
7
|
export interface Cache<T> {
|
|
8
8
|
current?: T;
|
|
9
9
|
previous?: T;
|