Live data from Hacker News

Things I wish I knew about state management when I started writing React apps

medium.com

31–40 of 335 posts

Re: Things I wish I knew about state management when I started writing React apps

#31
To manage state in our React application, we have a "service layer", just like you’d see in a server side application, only these are based on state machines (reactive services based on RxJS observables works as well).

We also created associated custom hooks that allow developers to easily "inject" a service into their React component (we use React's Context API for our "DI container").

From there developers can read the service state to drive the look of the UI, or send events to it to trigger behaviour that is encapsulated in the service itself.

Our most used service is a UserSession service that manages OAuth based login flow and session management.

The best part of this approach is that our service layer started in an Angular based proof-of-concept before we lifted-and-shifted it all into React.

[1] https://xstate.js.org/

Re: Things I wish I knew about state management when I started writing React apps

#32
post #5

Bounced around 2 projects in the last 12 months, one Angular and one React, that are using the Flux pattern. It is a real bitch to unit test anything but the individual pieces, which ends up being kind of meaningless.

Please explain the flux pattern?

Re: Things I wish I knew about state management when I started writing React apps

#33
post #26

> Think about the most complex frontends you’ve used. Frontends that made you wonder — “how did they create this”? What makes these frontends complex? State management. The frontend “knows” a lot of things, and these things interact with each other in non-trivial ways. So in my view, state management is the core problem when developing a UI. To build a non-trivial React application, you need to consider state managem…

You got that wrong. Modern JavaScript / SPAs make it possible to implement something like Outlook right within your browser. No (big) download, no clicking through an installation wizard, no fragmentation of versions for a tool, which forces you to be online anyway. I don't get all the hate towards SPAs and JavaScript Frameworks here on HN. Everyone is basically complaining about complexity for applications, which ar…

But is React simpler for building UIs than the RAD tooling of Visual Basic, Delphi or the Flash Designer?

Obviously there is the advantage of the web platform to leverage with JS frameworks, but there was something really nice about being able to drag and drop standard controls that all users were familiar with, and then just attach some code to them that talked to a database or whatever. I think that was the Parent's point that modern web development, while having the advantages of the web, is still more complicated than what was popular a decade or two ago for making UIs.

Re: Things I wish I knew about state management when I started writing React apps

#34

I'm starting to lean towards holding ALL app state in a single store rather than having component local state because every so often I get burned by not seeing the future. Example: okay I have a bunch of tabs that show different views. The state for which tab is selected can live with the tab container. Months later I find that I need other parts of the app to be able to switch to a different tab when user actions ha…

This is the way Elm works and I believe some people used or advocated for using Redux in this way early on. It never really caught on in React apps in my experience but it makes some interesting debugging methods easy to implement.

Re: Things I wish I knew about state management when I started writing React apps

#35
post #3

I’ve been learning React and have been avoiding Redux out of fear of complexity. However, as my app grows more complex, the need for a global state manager becomes more apparent. Time to start learning Redux today :)

1) Redux is pretty simple, but some of the terminology is obtuse for no good reason and makes it harder to get a grip on than it should be. "Action" = event, "Action Creator" = any function that emits an event, it's basically... not even a real thing worth discussing with its own special term. There, problem mostly solved. 2) TypeScript is the only sane way to use it. The bouncing between files and "wait, what was th…

Understanding the original context and intent of a tool is important. Back in 2017, I wrote a post that covered the overall intent behind Redux's design [0], as I was already seeing that folks didn't know the background. That lack of understanding behind Redux's history has become more apparent over time.

The terminology exists because Redux was originally designed as "just" another implementation of the Flux Architecture. Both "actions" and "action creators" were terms that had already been introduced by Flux [1]. There was considerable debate during the initial design phase about what terms to use, and the conclusion was to stick with Flux terminology to match the target audience of the time [2].

Note that the new Redux Style Guide docs page specifically recommends "modeling actions as 'events'" [3], using the "ducks" pattern for single-file Redux logic [4], and writing Redux logic using TypeScript [5].

In addition, our new official Redux Toolkit package [6] is our recommended approach for writing Redux logic. It has several utilities for common use cases like setting up the store, writing immutable updates, and creating slices of state, and it's written in TypeScript with an API designed to minimize the amount of type declarations you have to write. (In fact, I've even used its `createSlice()` function to write some fairly complex reducer logic that I used with React's `useReducer` hook.)

Finally, you _can_ reference multiple parts of the state tree in your reducer logic, but you have to explicitly set that up yourself [7]. The `combineReducers` utility is meant for the standard use case of defining update logic by domain "slices", and it's up to you to write custom logic if you need more specific behavior than that.

[0] https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-ta...

[1] https://facebook.github.io/flux/docs/in-depth-overview#actio...

[2] https://github.com/reduxjs/redux/issues/891#issuecomment-147...

[3] https://redux.js.org/style-guide/style-guide#model-actions-a...

[4] https://redux.js.org/style-guide/style-guide#structure-files...

[5] https://redux.js.org/style-guide/style-guide#use-static-typi...

[6] https://redux-toolkit.js.org

[7] https://redux.js.org/recipes/structuring-reducers/beyond-com...

Re: Things I wish I knew about state management when I started writing React apps

#37
post #32
post #5

Bounced around 2 projects in the last 12 months, one Angular and one React, that are using the Flux pattern. It is a real bitch to unit test anything but the individual pieces, which ends up being kind of meaningless.

Please explain the flux pattern?

See the docs for the original Flux Architecture:

https://facebook.github.io/flux/docs/in-depth-overview

Redux was originally designed as a Flux implementation:

https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-ta...

Re: Things I wish I knew about state management when I started writing React apps

#38

See also: Reagent. State is a first-class construct in Clojure/script, and forms associated with statefulness and state management are directly available. From that perspective, none of the front-ends presented appear complex. https://github.com/reagent-project/reagent

For more complex state handling in SPAs (subscription, event handling) re-frame is a really great framework based on reagent/cljs.

https://github.com/day8/re-frame

Re: Things I wish I knew about state management when I started writing React apps

#39
post #36

For anyone not familiar with UI app design, you do not need to always default to a Redux or Mobx. I've found a simple object / class with an event emitter will suffice for many cases.

yeah i used this for a while https://www.npmjs.com/package/tiny-state-manager

Re: Things I wish I knew about state management when I started writing React apps

#40

Earlier quoted context omitted.

1) Redux is pretty simple, but some of the terminology is obtuse for no good reason and makes it harder to get a grip on than it should be. "Action" = event, "Action Creator" = any function that emits an event, it's basically... not even a real thing worth discussing with its own special term. There, problem mostly solved. 2) TypeScript is the only sane way to use it. The bouncing between files and "wait, what was th…

Understanding the original context and intent of a tool is important. Back in 2017, I wrote a post that covered the overall intent behind Redux's design [0], as I was already seeing that folks didn't know the background. That lack of understanding behind Redux's history has become more apparent over time. The terminology exists because Redux was originally designed as "just" another implementation of the Flux Archite…

Yeah, I know the history of the terms, I just find them so entirely unhelpful for (harmful to, in fact) understanding Redux, no matter what reason they're there, that providing a translation is pretty much the first thing I do when introducing someone to it. Figuring out what the terms actually meant was the moment I went from "what... what is this thing doing?" to "oh it's a couple very simple things I already understand, no big deal, got it now".

> Finally, you _can_ reference multiple parts of the state tree in your reducer logic, but you have to explicitly set that up yourself [6]. The `combineReducers` utility is meant for the standard use case of defining update logic by domain "slices", and it's up to you to write custom logic if you need more specific behavior than that.

That's cool though.

Post reply on HN