reactish-state 0.11.0-alpha.0 → 0.11.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 +53 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Reactish-State
|
|
2
2
|
|
|
3
|
-
> Simple, decentralized state management for React.
|
|
3
|
+
> Simple, decentralized(atomic) state management for React.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -94,9 +94,9 @@ The state management solutions in the React ecosystem have popularized two state
|
|
|
94
94
|
|
|
95
95
|
- **Centralized**: a single store that combines entire app states together and slices of the store are connected to React components through selectors. Examples: react-redux, Zustand.
|
|
96
96
|
|
|
97
|
-
- **Decentralized**: consisting of many small states which can build up state dependency trees using a bottom-up approach. React components only need to connect with the states that they use. Examples: Recoil, Jotai.
|
|
97
|
+
- **Decentralized**: consisting of many small(atomic) states which can build up state dependency trees using a bottom-up approach. React components only need to connect with the states that they use. Examples: Recoil, Jotai.
|
|
98
98
|
|
|
99
|
-
This library adopts the decentralized state model, offering a _Recoil-like_ API, but with a much simpler and smaller implementation(similar to Zustand), which makes it the one of the smallest state management solutions with gzipped bundle size
|
|
99
|
+
This library adopts the decentralized state model, offering a _Recoil-like_ API, but with a much simpler and smaller implementation(similar to Zustand), which makes it the one of the smallest state management solutions with gzipped bundle size around 1KB.
|
|
100
100
|
|
|
101
101
|
| | State model | Bundle size |
|
|
102
102
|
| --- | --- | --- |
|
|
@@ -153,7 +153,7 @@ The difference might sound insignificant, but imaging every single state update
|
|
|
153
153
|
- Feature extensible with middleware or plugins
|
|
154
154
|
- States persistable to browser storage
|
|
155
155
|
- Support Redux dev tools via middleware
|
|
156
|
-
-
|
|
156
|
+
- [~1KB](https://bundlephobia.com/package/reactish-state): simple and small
|
|
157
157
|
|
|
158
158
|
# Recipes
|
|
159
159
|
|
|
@@ -303,6 +303,55 @@ const Example = () => {
|
|
|
303
303
|
};
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
+
## Selector that depends on props or local states
|
|
307
|
+
|
|
308
|
+
The `selector` function allows us to create reusable derived states outside React components. In contrast, component-scoped derived states which depend on props or local states can be created by the `useSelector` hook.
|
|
309
|
+
|
|
310
|
+
```jsx
|
|
311
|
+
import { state, useSelector } from "reactish-state";
|
|
312
|
+
|
|
313
|
+
const todosState = state([{ task: "Shop groceries", completed: false }]);
|
|
314
|
+
|
|
315
|
+
const FilteredTodoList = ({ filter = "ALL" }) => {
|
|
316
|
+
const filteredTodos = useSelector(
|
|
317
|
+
() => [
|
|
318
|
+
todosState,
|
|
319
|
+
(todos) => {
|
|
320
|
+
switch (filter) {
|
|
321
|
+
case "ALL":
|
|
322
|
+
return todos;
|
|
323
|
+
case "COMPLETED":
|
|
324
|
+
return todos.filter((todo) => todo.completed);
|
|
325
|
+
case "ACTIVE":
|
|
326
|
+
return todos.filter((todo) => !todo.completed);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
],
|
|
330
|
+
[filter]
|
|
331
|
+
);
|
|
332
|
+
// Render filtered todos...
|
|
333
|
+
};
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
The second parameter of `useSelector` is a dependency array (similar to React's `useMemo` hook), in which you can specify what props or local states the selector depends on. In the above example, `FilteredTodoList` component will re-render only if the global `todosState` state or local `filter` prop have been updated.
|
|
337
|
+
|
|
338
|
+
### Linting the dependency array of useSelector
|
|
339
|
+
|
|
340
|
+
You can take advantage of the [eslint-plugin-react-hooks](https://www.npmjs.com/package/eslint-plugin-react-hooks) package to lint the dependency array of `useSelector`. Add the following configuration into your ESLint config file:
|
|
341
|
+
|
|
342
|
+
```json
|
|
343
|
+
{
|
|
344
|
+
"rules": {
|
|
345
|
+
"react-hooks/exhaustive-deps": [
|
|
346
|
+
"warn",
|
|
347
|
+
{
|
|
348
|
+
"additionalHooks": "useSelector"
|
|
349
|
+
}
|
|
350
|
+
]
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
306
355
|
## Still perfer Redux-like reducers?
|
|
307
356
|
|
|
308
357
|
```js
|