Search⌘ K

Refactoring to the Reducer Pattern

Explore how to manage state more effectively in Stimulus code by refactoring to the reducer pattern. Understand the benefits of using immutable state and structured actions to maintain cleaner, more predictable state changes in your front-end application.

We'll cover the following...

Current issues

While there’s a lot to like about the previous solution, it has a couple of issues that, while not necessarily a problem at this size, might become a problem if the code gets more complex. For example:

  • Each individual action needs to know to call calendarFilterChanged to trigger a refresh.
  • The actual state is global and mutable, which means if some piece of code grabs the data and starts writing it, that change is made to everybody else, whether we want it to or not.

Refactoring

In ...