You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- React is no more a `View` lib, it's now (v17) a complete framework: so either we pick a lighter lib for the `View`, or choosing React ❌ **we shouldn't need to use an additional external framework** such as Redux, MobX, RxJs, Recoil, Zustand, Jotail...
4
+
5
+
- A first approach could be to use local states and sporadic use of React context, like [explained here by Kent C. Dodds](https://kentcdodds.com/blog/application-state-management-with-react), but ❌ it's not a proper Flux implementation, I'd rather have **my entire app state fully separated from the `View`**, and "connect" [containers](https://medium.com/@learnreact/container-components-c0e67432e005), mapping sub-states to the views, the way Redux allows to.
6
+
7
+
- Using React context to propagate the global app state, like [suggested here by Rico Sta. Cruz](https://ricostacruz.com/til/state-management-with-react-hooks), [or here by Ebenezer Don](https://blog.logrocket.com/use-hooks-and-context-not-react-and-redux/), would be ok for a _small application_, but would quickly lead to ❌ **tons of useless re-renderings**.
8
+
9
+
- That would eventually lead to lots of specific `useMemo` on every component requiring performance optimisation.
10
+
So rather than to put the effort on developping on a proper state/component architecture, your effort will be spent on ❌ **writing those `useMemo` everywhere**.
11
+
12
+
- Eventually, all these steps lead me to `RxJs` which allows the use of **many stores**, by subscribing to their updates on `useEffect` and applying changes with `useState` I would have this **local rerendering** I want.
13
+
❌ Well, that would mean adding a third party lib, and I'd like not to.
14
+
15
+
- Finally, I've come accross [zustand](https://github.com/pmndrs/zustand), which resolves all these previous points and use React hooks only !
16
+
That is the lib **the closest** to the way I want to manage my state, but ❌ I want to keep my redux-like reducers, actions and container connect function to map my stores locally to the required props for the finest re-rendering, and no extra-recipes (back to point 1)
17
+
18
+
## 🧙 experimentation
19
+
20
+
The idea with `Hookstores` is
21
+
22
+
- ✅ to stay within React **only**,
23
+
- ✅ to implement a simple [Flux architecture](https://facebook.github.io/flux/docs/in-depth-overview)
24
+
- ✅ **splitting** the global app state into **stores** states,
25
+
- ✅ applying **local rendering**, by mapping these stores states to [containers](https://medium.com/@learnreact/container-components-c0e67432e005), using React hooks `useState` and `useEffect`.
26
+
- ✅ using React hooks to create the stores on app startup, then access `{dispatch, ...stores} = useStores()` everywhere:
27
+
28
+
-`dispatch` allowing to emit actions to every store, and they now if they have to compute this action to reduce a new state.
29
+
30
+
- your `stores` are accessible, with the `key` you give in `descriptions`, to allow kind of "mapStateToProps" on the connected containers, only when there _is_ an update: Now that's local re-rendering.
31
+
32
+
## ☢️ disclaimer
33
+
34
+
So yes, somehow it ends up as another lib to manage your React state 🙃.
35
+
36
+
But since it's only few files you should understand what's behind the hood, then use and tweak them to your convenience _within your own React app_ rather than use it out of the box.
37
+
38
+
You'll see `Hookstores` is rather few lines to **patch** React.
39
+
40
+
Furthermore,
41
+
42
+
- ⚠️ it's not written in typescript 🙀
43
+
- ⚠️ there are no tests 💥
44
+
45
+
That being said,
46
+
47
+
- ✅ I'm confidently using this implementation between many apps,
- React is no more a `View` lib, it's now (v17) a complete framework: so either we pick a lighter lib for the `View`, or choosing React ❌ **we shouldn't need to use an additional external framework** such as Redux, MobX, RxJs, Recoil, Jotail...
16
-
17
-
- A first approach could be to use local states and sporadic use of React context, like [explained here by Kent C. Dodds](https://kentcdodds.com/blog/application-state-management-with-react), but ❌ it's not a proper Flux implementation, I'd rather have **my entire app state fully separated from the `View`**, and "connect" [containers](https://medium.com/@learnreact/container-components-c0e67432e005), mapping sub-states to the views, the way Redux allows to.
18
-
19
-
- Using React context to propagate the global app state, like [suggested here by Rico Sta. Cruz](https://ricostacruz.com/til/state-management-with-react-hooks), [or here by Ebenezer Don](https://blog.logrocket.com/use-hooks-and-context-not-react-and-redux/), would be ok for a _small application_, but would quickly lead to ❌ **tons of useless re-renderings**.
20
-
21
-
- That would eventually lead to lots of specific `useMemo` on every component requiring performance optimisation.
22
-
So rather than to put the effort on developping on a proper state/component architecture, your effort will be spent on ❌ **writing those `useMemo` everywhere**.
23
-
24
-
- Eventually, all these steps lead me to `RxJs` which allows the use of **many stores**, by subscribing to their updates on `useEffect` and applying changes with `useState` I would have this **local rerendering** I want.
25
-
❌ Well, that would mean adding a third party lib, and I'd like not to.
26
-
27
-
## 🧙 experimentation
28
-
29
-
The idea with `Hookstores` is
30
-
31
-
- ✅ to stay within React **only**,
32
-
- ✅ to implement a simple [Flux architecture](https://facebook.github.io/flux/docs/in-depth-overview)
33
-
- ✅ **splitting** the global app state into **stores** states,
34
-
- ✅ applying **local rendering**, by mapping these stores states to [containers](https://medium.com/@learnreact/container-components-c0e67432e005), using React hooks `useState` and `useEffect`.
35
-
- ✅ using React context only to provide `Hookstores` with `{dispatch, ...stores}` everywhere,
36
-
37
-
-`dispatch` allowing to emit actions to every store, and they now if they have to compute this action to reduce a new state.
38
-
39
-
- your `stores` are accessible, with the `key` you give in `descriptions`, to allow kind of "mapStateToProps" on the connected containers, only when there _is_ an update: Now that's local re-rendering.
40
-
41
-
## ☢️ disclaimer
42
-
43
-
So yes, somehow it ends up as another lib to manage your React state 🙃.
44
-
45
-
But since it's only few files you should understand what's behind the hood, then use and tweak them to your convenience _within your own React app_ rather than use it out of the box.
46
-
47
-
You'll see `Hookstores` is rather few lines to **patch** React.
48
-
49
-
Furthermore,
50
-
51
-
- ⚠️ it's not written in typescript 🙀
52
-
- ⚠️ there are no tests 💥
53
-
54
-
That being said,
55
-
56
-
- ✅ I'm confidently using this implementation between many apps,
57
-
- ✅ so I prefer to have this package,
58
-
- ✅ so why not sharing this experiment.
15
+
read this [doc]('docs/motivation.md')
59
16
60
17
---
61
18
@@ -69,15 +26,12 @@ That being said,
69
26
70
27
## 🛠 setup
71
28
72
-
Under the hood, the `Hookstores` uses [React Context](https://reactjs.org/docs/context.html).
29
+
Here is the path to follow to setup Hookstores on your app:
73
30
74
-
As such, you can wrap your root component, such as `App`, with the provider called `Hookstores` for convenience, to integrate it with your React app.
75
-
76
-
```jsx
77
-
<Hookstores>
78
-
<App />
79
-
</Hookstores>
80
-
```
31
+
- 1: write descriptions: your redux-like reducers
32
+
- 2: create stores on startup
33
+
- 3: bind store data to your container state
34
+
- 4: dispatch actions to trigger stores changes
81
35
82
36
---
83
37
@@ -89,10 +43,12 @@ In the following, let's illustrate how to use `Hookstores` with:
89
43
90
44
---
91
45
92
-
## ✍️ descriptions
46
+
## 1 ✍️ descriptions
93
47
94
48
`Hookstores` will create stores, register for actions, and emit updates using the descriptions you provide.
🔍 Don't forget to return the `disconnect` function at the end of your hook, unless you will have stores updates triggering unmounted containers updates.
209
172
210
173
---
211
174
212
-
## 📡 dispatching actions
175
+
## 4 📡 dispatching actions
213
176
214
177
Use [`prop drilling`](https://kentcdodds.com/blog/prop-drilling) from your containers to your components: pass functions dispatching the actions
0 commit comments