by superice · 3y ago · 0 comments
I always find that if I want to write an app in the Elm style (or with Redux), I have to traverse up the tree every time I am working on a leaf node. Each input field in a form should not necessarily cause me to have to rework everything up to the root node right? Am I doing it wrong, or am I just missing what benefits this approach brings to offset this extra development cost?
Especially for server-centric CRUD apps with forms and lists, this is often abused. If you're reloading lists when navigating to them, and fetching form data when updating a record, then you don't really need global state. If you want to cache, perhaps a plain cache is enough.
If you need some piece of data to affect multiple components in different parts of the hierarchy, sure, make it global, but if you can get away with mostly local state the codebase will be simplified. IMO.
There's zero need to make everything be the same. There's not even the need to keep state in one single place: you can control all the state and its transitions locally but then "copy" to state management if you need it somewhere else. This is a perfectly valid solution too.
Another problem of using global state for everything is that it reduces the possibility of using a lot of component-level or hook-level abstractions that would be otherwise very useful. You're now forced to do everything through Redux or something like that.
There's no need to rush for an "absolute" solution with all that overhead as if it was a golden bullet. It's not.
the issue is that current frameworks make global state awkward to pass around. if this were fixed then global has all of the advantages.
I agree that this project misses the mark.
In Elm specifically, you should not have state in the form elements. They should only have component-specific update functions like "Checked" / "Unchecked" that are mapped to form level messages and of course the view function.
And no, you don't need to to prop drilling when using contexts or a state manager.
Anyways, the tide turned again and everyone said, "functional programming is the bees knees! [don't mind these strange hooks that inject side-effects and state]". Then they said, why are we storing state in these pure functional entities. Let's separate out the state and the control..
Back to model-view-controller. Yay?
The root node can intercept all messages but its main responsibility (TEA) is to dispatch the messages to the right component(s).
(Each component consists of a view, a model and an update function, the model contains wrapped versions of the children models and dispatched updates get passed into the children updates)
F(f_1(x_1), f_2(x_2), …, f_n(x_n))
The root function is a collection of projections acting on (x_1, x_2, …).
And the components receive the right set of coordinates to act on.
I’m not sure if it’s clearer that way though.