by dovel · 7y ago · 0 comments
Do I want this state to survive a page reload, new browser session, or coming from a bookmark?
The value in a drop-down box in some random form? Non-global state.
The main page active tab that they were probably reading? Global state.
Scroll position? Global.
Tooltip Position? Probably not useful, non-global.
Something like a dialog or modal depends on the situation of how important it was and if the user would expect it to be there.
That makes me see that there isn't a "god" object like the article calls it, that holds all of the app state. There's the root state shared throughout the app, and then each page or component can have its own encapsulated state.
Ideally I like to also keep page- or component-specific state+actions decoupled from their views, functional and testable independently - but often I start/keep them in the component class (and maybe soon hooks).
But your question makes me think, if I store any UI state in the app state, have I really decoupled state and view? I'd say yes - since I'd be able to build and test UI state and actions, independent from the view.
Then when you expand to an API or CLI interface the web state being tracked no longer makes sense and might be extra ram or loading time.
In practice I haven't used it so much, but I like the idea of treating the UI as just another render target / consumer of app state/actions. It enables "remote control" of the app logic, makes testing simpler, and allows reuse for various interfaces like CLI, API, maybe React Native too.
I've been playing around switching web-apps and other user programs between devices on a single-ususer facing kubernetes system. Part of that relies on all state being sent to the server for distribution if a device switch happens.
I also have played around with a wrapper functions and that can bubble any callback/hook up to the data for further processing. The client no longer worries about sending actions. The server spys on client actions and performs actions on their behalf.
I plan on posting a blog post about the his soon.