Codementor Events

Fundamental idea of Redux

Published May 26, 2018

Online tutorials on Redux follow the same pattern:

  • Basic concepts and terminology
  • Example project using redux
  • Best practices (eg separating actions into different files)

There are several drawbacks to this strategy but there is one which fundamentally holds back the learner. All the lessons I've seen skip over the big question that should guides the learning of the subject: why is this even needed?

Common answers might be:

  • Avoiding incomprehensible prop chains
  • Separation of data and views
  • Ability to access previous states

However, they all these proposed benefit ignore this simple fact: they can be achieved more simply without the use of actions and reducers. So why are these iconic features so important? It's because actions and reducers give you the benefit of global variables without the perils.

In short, global variables give a programmer some power but it also gives the programmer many more responsibilities. In most cases, the cost of the responsibilities far outway the power.

Global variables are very useful because they can be read anywhere. Programmers really love this. It's extraordinarily useful. But what about the responsibilities?

Programmers hate global variables because they make the program hard to read and are the source of numerous bugs. The amount of discipline and structure needed to manage them are often not worth the benefit. And the fundamental reason for this is that the data pointed to by the global variable can be modified anywhere.

So we love the fact that we can read global variables anywhere but we hate the fact that we can mutate the data from anywhere. Is it possible to have the convenience without the cost? This is what actions and reducers provide.

Redux forces us to explicitly state how the global variable is modified by having us write it out in the reducer. Reducers are defined in a centralized location, so they can be referred to, modified and reasoned about much more easily.

In other words, redux gives you most of the power of global variables and removes most of the responsibilities.

Discover and read more posts from Jacques Le Normand
get started
post commentsBe the first to share your opinion
Show more replies