Search code examples
reactjsjsonreduxstatestate-management

What are the benefits of using redux over a series of global json objects for data storage?


After working with/tutoring redux for a few years I have a question about redux.

Why use redux over storing global state in a series of JSON objects?

You could make rest calls and easily store key values pairs in the global state object.

There is a lot of boilerplate and terminology with redux that makes it hard to adopt.

With JSON objects there is a lot less complex code and it's easier to manage, or at least it seems that way.

I feel like state management doesn't have to be as structured as redux.

I'm curious what others think about this?


Solution

  • The JSON object

    • cannot tell you when it was updated, so you have to check for that all the time (React will not rerender if you change it)
    • can be changed from everywhere without protection (you will end up with spaghetti code where you just see that things change, but have no idea why they change or how they change)

    Redux is not a global variable, but the place where your logic should live. You should not have a bunch of "setter actions" and calculate new values somewhere in your app - you should just dispatch a "ThisEventHappened" action, and your state changes accordingly & updates your app.

    That said, I have the feeling that you are talking about an outdated (pre-2019) style of Redux here. Modern Redux doesn't have a lot of boilerplate - it doesn't use switch..case reducers, ACTION_TYPES, immtuable update logic, createStore/applyMiddleware or connect/mapStateToProps. Please read up on modern Redux and follow the official tutorial as many other tutorials still teach the old style which is 4 times the amount of code.