|
|
|
|
|
by patrickthebold
322 days ago
|
|
I've been toying with an idea of a pattern. I'm curious as to if it has a name. I'll write a blog post once I have an app using it. In the meantime, it's (roughly): - No Hooks.
- Don't try to sync any state. E.g. keep a dom element as the source of truth but provide access to it globally, let's call this external state.
- Keep all other state in one (root) immutable global object.
- Make a tree of derived state coming from the root state and other globally available state. (These are like selectors and those computations should memoized similar to re-select)
- Now imagine at any point in time you have another tree; the dom tree. If you try to make the state tree match to dom tree you get prop drilling.
- Instead, flip the dom tree upside down and the leaves get their data out of the memoized global state.
- Parent components never pass props to children, the rendered children are passed as props to the parent.
You end up with a diamond with all state on the top and the root of the dom tree on the bottom.One note, is that the tree is lazy as well as memoized, there's potentially many computations that don't actually need to be computed at all at any given time. You need something like Rx to make an observable of the root state, some other tools to know when the external state changes. Some memoization library, and the react is left with just the dom diffing, but at that point you should swap out to a simpler alternative. |
|
https://legendapp.com/open-source/state/v3/