Step by Step Guide to Building React Redux Apps
medium.com
medium.com
It is only a tool you should reach for after you find you need it. Too few developers approach software development with this level of pragmatism and instead we get "use X because it's being used". I guess you need to understand the tool to know when to use it - but that doesn't mean it should become the "defacto standard".
Ironically Redux is being coupled with React, which is a great library for structuring UI into components and resulting it simpler to understand code.
function createStore(reducer, initialState) { let state = initialState; return { dispatch(action) { state = reducer(state, action); }, getState() { return state; } } }
It's quite simple.. and yes, it should only be used when needed, but the whole implementation of the Redux part of an app can exist in a few lines in a single file.
Furthermore, Redux itself is a relatively small library, written in a heavily functional style. True, this may be hard to wrap one's head around for developers new to this, but I can think of no better place to learn due to not only the small size, but a lot of documentation and _very_ active involvement (in GH issues and StackOverflow) by it's author.
I'll take this as an opportune time to link to two very helpful and useful posts about Redux from its author (Dan Abramov) about upsides (http://stackoverflow.com/questions/32461229/why-use-redux-ov...) and downsides (http://stackoverflow.com/questions/32021763/what-could-be-th...) of using Redux.
Namely, I see Redux offering a nice advantage over flux in encouraging reducer composition (and functional purity) for modularity. "This pattern also enables wonderful features like no-user-code undo/redo. Can you imagine plugging Undo/Redo into a Flux app being two lines of code? Hardly. With Redux, it is—again, thanks to reducer composition pattern. I need to highlight there's nothing new about it—this is the pattern pioneered and described in detail in Elm Architecture which was itself influenced by Flux."
In short, the key value add for Redux, is not necessarily that it's (overly/inadequately) decoupled, but that it is necessarily forcing a decoupling via functional purity for the /purpose/ of modularity. That is, it is taking a more functional approach to state management for React components, favoring the event log paradigm rather than black box paradigm. This brings with it the ability to be "designed with use cases such as logging, support for Promises, Observables, routing, immutability dev checks, persistence, etc, in mind." Of course, these aren't impossible with Flux, but these things follow intrinsically from Redux.
Take my opinion with a grain of salt because I'm not super experienced with Redux yet, but I really like what I've seen so far playing with it.
It will take a person a couple of days to "grok" Redux, and a quite a bit longer to fully understand the ins and outs so one can make informed, confident decisions on how to solve problems "the Redux way".
Now you gain "cool" things by doing it, but here is the catch: Is the benefit worth the time investment to get there?
His argument is that for a lot of simple apps it isn't. You get very far with plain React, and a project has to have significant complexity to pay back the costs of fully understanding Redux.
I would argue a lot of people picking the React ecosystem make the mistake of choosing too much architectural complexity, not too little.
The extreme pluggability helps to broaden usage of Redux. But it is overkill for many projects. Decoupling is important, but it is not free (see parts of the Java ecosystem).
Devs should consciously decide what to introduce, and leave out stuff that simply doesn't increase development efficiency given the nature of their project.
When you find it hard to understand your state management within some parent react containers, or you're sharing a lot of state between siblings, consider flux or redux. Not before.
But it's really extremely simple. The terminology is perhaps more abstract than one might prefer, but I have yet to think of a way to improve upon it.
Also, if you let most components be pure, it's easy to start with a stateful component or two and then add redux later.
Here is the same app in 130 lines:
https://github.com/techlayer/espresso.js/blob/master/example...
I don't think the author advocates using react-redux for the simplest todo list app.
export const addTodo =
text => ({
type: 'ADD_TODO',
id: nextTodoId++,
text,
completed: false
});
export const setVisibilityFilter =
filter => ({type: 'SET_VISIBILITY_FILTER', filter});
export const toggleTodo =
id => ({type: 'TOGGLE_TODO', id});If you have any FP background, this is really nothing innovative, more of a common sense kind of thing. With the main achievement being that it's trivial enough and has nice documentation with examples so that mass JS programmers can actually grok it...
I've nothing against Redux, it's a good idea but quite a bit overhyped is all.
In Redux, it seems to me, there's no way you can do it, or at least nothing is provided out of the box. Am i correct?
componentDidMount() {
item = store.getState().get('item') // Get function from Immutable.js
this.unsubscribe = store.subscribe(() => {
let nowItem = store.getState().get('item');
if (item !== nowItem) {
this.forceUpdate();
item = nowItem;
}
});
}
Someone may have a better solution, but I think this works fine for my needs. The internet is full of people dissuading redux users from subscribing to actions.The examples mostly use things like the spread operator and Object.assign.
I would say redux differs from baobab in not enforcing a state structure that mirrors the component tree. So it might be that if your state structure matches the component tree baobab is the simpler way, and redux is better if you need to decouple them.
I'm using redux with a structure nothing like the component tree. I ended up keeping updates and the tree trivial and keep all logic related to how to present the data in selectors (Reselect). Ending up with a structure not unlike the reflux implementation i was migrating from ;) to isolate parts of the state i have all subtrees export getters and actions so that reselect selectors works with the root getter for the subtree.