React, Redux and the Observer Pattern

React, Redux and the Observer Pattern

Would you happen to know what pattern Redux uses?


Image Source - pixabay


Redux doesn't explicitly follow the Observer, Factory, or Singleton patterns. However, it elegantly implements the core principles of the Observer pattern to manage state and UI updates efficiently.


Understanding the Observer Pattern

The Observer pattern establishes a one-to-many dependency between objects, where changes in one object (subject) notify its dependents (observers). This pattern is crucial for building reactive and responsive applications.


Let's understand how the Observer pattern is used by React JS in rendering frontend UI and how Redux manages the app state.


Image Source - uxwing

React is all about components, states and props.

Components are modular representations of the UI.

States are the information (Javascript object).

props are passed to another component.


What if the Parent wants to pass the state to the Grandchild directly without having to re-render the intermediate Child? This is where Redux steps in.

Source Image -


In React, whenever a state within a component changes, the component is re-rendered. When this state is passed to a child as a prop, this child component will be re-rendered when the prop changes.

This holds recursively.

If the state in Parent is passed down to Grandchild, a change in the state will cause all components holding that state to be re-render. Here, the observers are the components holding the state; the Parent is the subject notifying the observers.

Redux to the rescue!

Store - In Redux, states are stored in somewhere called Store. It's like a container that holds your application's global state.


Redux and the Observer Pattern

In Redux, the store acts as the subject. It holds the application's state. Components are the observers that subscribe to changes in the store.

How it works:

  1. Components subscribe to the store: Using connect from react-redux for integrating Redux and React together, components can subscribe to parts of the store they're interested in.
  2. State updates: When an action is dispatched, reducers modify the state, creating a new state object.
  3. Notification and re-rendering: The store notifies subscribed components about the state change. These components re-render to reflect the updated data.


Diagrammatic Representation by Redux

Image Source -


Redux Observer Pattern

  • Store: The central hub holding the application's state.
  • Components: Multiple components subscribing to different parts of the store.
  • Actions: Events that trigger state updates.
  • Reducers: Functions that determine how the state changes based on actions.


Key Benefits of Redux's Observer-like Behavior

  • Predictability: State changes are always triggered by actions, making the application's behaviour easier to reason about.
  • Efficiency: Components only re-render when their subscribed data changes, optimizing performance.
  • Testability: The unidirectional data flow and pure functions make testing easier.
  • Scalability: Redux can handle complex state management in large applications.


In conclusion, while Redux doesn't strictly adhere to the Observer pattern, it effectively leverages its core principles to create a robust and predictable state management solution. Understanding this underlying pattern is essential for mastering Redux and building efficient React applications.


#redux #observerpattern #react #statemanagement #javascript #webdevelopment #reactjs #programming #frontend #fullstack #frontenddevelopment #learning #reactandredux #connections

要查看或添加评论,请登录

Neeraj Swarnkar的更多文章

社区洞察

其他会员也浏览了