Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't Redux just glorified global state?

So I started learning React a week ago and I inevitably got to the problem of state and how components are supposed to communicate with the rest of the app. I searched around and Redux seems to be the flavor of the month. I read through all the documentation and I think it's actually a pretty revolutionary idea. Here are my thoughts on it:

State is generally agreed to be pretty evil and a large source of bugs in programming. Instead of scattering it all throughout your app Redux says why not just have it all concentrated in a global state tree that you have to emit actions to change? Sounds interesting. All programs need state so let's stick it in one impure space and only modify it from within there so bugs are easy to track down. Then we can also declaratively bind individual state pieces to React components and have them auto-redraw and everything is beautiful.

However, I have two questions about this whole design. For one, why does the state tree need to be immutable? Say I don't care about time travel debugging, hot reload, and have already implemented undo/redo in my app. It just seems so cumbersome to have to do this:

case COMPLETE_TODO:   return [     ...state.slice(0, action.index),     Object.assign({}, state[action.index], {       completed: true     }),     ...state.slice(action.index + 1)   ]; 

Instead of this:

case COMPLETE_TODO:   state[action.index].completed = true; 

Not to mention I am making an online whiteboard just to learn and every state change might be as simple as adding a brush stroke to the command list. After a while (hundreds of brush strokes) duplicating this entire array might start becoming extremely expensive and time-consuming.

I'm ok with a global state tree that is independent from the UI that is mutated via actions, but does it really need to be immutable? What's wrong with a simple implementation like this (very rough draft. wrote in 1 minute)?

var store = { items: [] };  export function getState() {   return store; }  export function addTodo(text) {   store.items.push({ "text": text, "completed", false}); }  export function completeTodo(index) {   store.items[index].completed = true; } 

It's still a global state tree mutated via actions emitted but extremely simple and efficient.

like image 544
Ryan Peschel Avatar asked Oct 29 '15 20:10

Ryan Peschel


People also ask

Is Redux just global state?

With Redux, state is stored globally and can easily be updated or invoked from anywhere in the app.

Why do we need a global state in Redux?

The primary advantage of global state for a user-interface application is that you can track state changes of the entire application ATOMICALLY. tldr; you can always save and predict the app's state easily because there is a single source of truth.

Should I use Redux for everything?

Using Redux also means learning how it works, which again could be a waste of time if you don't need it. As a rule of thumb - and one shared by one of Redux's creators, Dan Abramov - you don't need to use Redux unless you're unable to manage state within React or other front-end frameworks you're working with.

What does Redux actually do?

Redux simply provides a subscription mechanism which can be used by any other code. That said, it is most useful when combined with a declarative view implementation that can infer the UI updates from the state changes, such as React or one of the similar libraries available.


1 Answers

Isn't Redux just glorified global state?

Of course it is. But the same holds for every database you have ever used. It is better to treat Redux as an in-memory database - which your components can reactively depend upon.

Immutability enables checking if any sub-tree has been altered very efficient because it simplifies down to an identity check.

Yes, your implementation is efficient, but the entire virtual dom will have to be re-rendered each time the tree is manipulated somehow.

If you are using React, it will eventually do a diff against the actual dom and perform minimal batch-optimized manipulations, but the full top-down re-rendering is still inefficient.

For an immutable tree, stateless components just have to check if the subtree(s) it depends on, differ in identities compared to previous value(s), and if so - the rendering can be avoided entirely.

like image 123
lorefnon Avatar answered Oct 21 '22 10:10

lorefnon