pocket-state 0.0.4 → 0.0.5
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 +126 -0
- package/package.json +2 -2
package/README
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# Pocket State
|
|
2
|
+
|
|
3
|
+
A lightweight state management library for React, built on top of `useSyncExternalStore`.
|
|
4
|
+
Designed to be **tiny, fast, and predictable**
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Install with your favorite package manager:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# npm
|
|
14
|
+
npm install pocket-state
|
|
15
|
+
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add pocket-state
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## API
|
|
24
|
+
|
|
25
|
+
### `createStore<T>(initialState: T, middlewares?: Middleware<T>[])`
|
|
26
|
+
|
|
27
|
+
Create a new store.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import {createStore} from 'pocket-state';
|
|
31
|
+
|
|
32
|
+
type Counter = {count: number};
|
|
33
|
+
|
|
34
|
+
const counterStore = createStore<Counter>({count: 0});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
### `useStore(store, selector?, equalityFn?)`
|
|
40
|
+
|
|
41
|
+
Subscribe to store state inside a React component.
|
|
42
|
+
It only re-renders when the selected slice changes.
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import {useStore} from 'pocket-state';
|
|
46
|
+
import {counterStore} from './counterStore';
|
|
47
|
+
|
|
48
|
+
function CounterDisplay() {
|
|
49
|
+
const count = useStore(counterStore, s => s.count);
|
|
50
|
+
return <Text>Count: {count}</Text>;
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### `useShallowStore(store, selector)`
|
|
57
|
+
|
|
58
|
+
Like `useStore` but uses shallow equality. Useful when selecting an object.
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
import {useShallowStore} from 'pocket-state';
|
|
62
|
+
|
|
63
|
+
function Info() {
|
|
64
|
+
const {count, active} = useShallowStore(counterStore, s => ({
|
|
65
|
+
count: s.count,
|
|
66
|
+
active: s.active,
|
|
67
|
+
}));
|
|
68
|
+
return (
|
|
69
|
+
<Text>
|
|
70
|
+
{count} {active ? 'ON' : 'OFF'}
|
|
71
|
+
</Text>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
### Store API
|
|
79
|
+
|
|
80
|
+
Each store exposes:
|
|
81
|
+
|
|
82
|
+
- `getValue()` → current state
|
|
83
|
+
- `setValue(updater)` → update state (`updater` can be partial or producer)
|
|
84
|
+
- `reset(nextState?)` → reset to initial or custom state
|
|
85
|
+
- `subscribe(listener)` → subscribe to all changes
|
|
86
|
+
- `getNumberOfSubscriber()` → count active subscribers
|
|
87
|
+
|
|
88
|
+
Example:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
counterStore.setValue(s => ({count: s.count + 1}));
|
|
92
|
+
const state = counterStore.getValue();
|
|
93
|
+
console.log(state.count); // → 1
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Example
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import React from 'react';
|
|
102
|
+
import {Text, Button, View} from 'react-native';
|
|
103
|
+
import {createStore, useStore} from 'pocket-state';
|
|
104
|
+
|
|
105
|
+
// Create store
|
|
106
|
+
const counterStore = createStore({count: 0});
|
|
107
|
+
|
|
108
|
+
export default function App() {
|
|
109
|
+
const count = useStore(counterStore, s => s.count);
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<View>
|
|
113
|
+
<Text>Count: {count}</Text>
|
|
114
|
+
<Button
|
|
115
|
+
title="Increment"
|
|
116
|
+
onPress={() => counterStore.setValue(s => ({count: s.count + 1}))}
|
|
117
|
+
/>
|
|
118
|
+
<Button title="Reset" onPress={() => counterStore.reset()} />
|
|
119
|
+
</View>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
🔥 That’s it — no boilerplate, no context providers.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pocket-state",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "tiny global store",
|
|
5
5
|
"main": "src/index",
|
|
6
6
|
"codegenConfig": {
|
|
@@ -10,5 +10,5 @@
|
|
|
10
10
|
},
|
|
11
11
|
"author": " <@kayda69> (nhh.tcp@gmail.com)",
|
|
12
12
|
"license": "ISC",
|
|
13
|
-
"homepage": "
|
|
13
|
+
"homepage": "README"
|
|
14
14
|
}
|