In particular React developers have been hit hard via this phenomenon, as it gave everybody a clear path on how to deal with kingdom control.
12 months later, Dan Abramov gave a recap on what made Redux successful within the first vicinity. each talk is incredibly insightful on how trouble can be solved with a bit of technology and what makes this technological lasting in the end. plenty of JavaScript libraries come and move. however Redux controlled to live with us.
however, I believe there is extra than simply one successful library. Redux is a whole mindset shift for lots of people inside the JavaScript community, who literally grew up with the simplest internet improvement, however by no means heard approximately feature composition or immutability before. whether Redux stays with us for a few more years or makes area for other country management libraries, it leaves a wonderful legacy on how we develop modern web programs.
Everything HAS A clean motive
If someone would ask me for one short representation of Redux, it would be:
State => View
If it needs more explanation, I would extend it into:
Action => Reducer(s) => Store => View
If there is more context needed, one could extend it into a repeating loop:
Action => Reducer(s) => Store => View => User Interaction => Action ...
That's all of Redux (State) in the context of a library like React (View). Every part in the chain has its task. Everything is clearly separated from each other and serves a purpose for the greater goal: state management.
However, too many people associate Redux tightly with React. Once they start learning React, they go all-in by combining React with Redux from the start which demotivates lots of developers with its complexity. However, Redux, in a nutshell, isn't that complex, if just considering Redux, because after all, it's just a state container (object) which holds state; with an API that enables one
Let's recap all parts of Redux briefly in JS. This is a Redux Reducer that acts on two Redux Actions which has no dependencies on the Redux library at all:
function reducer(state, action) { switch(action.type) { case 'TODO_ADD' : { return applyAddTodo(state, action); } case 'TODO_TOGGLE' : { return applyToggleTodo(state, action); } default : return state; }}function applyAddTodo(state, action) { return state.concat(action.todo);}function applyToggleTodo(state, action) { return state.map(todo => todo.id === action.todo.id ? { ...todo, completed: !todo.completed } : todo );}
The Redux store which knows about the Redux Reducer:
import { createStore } from 'redux';const store = createStore(reducer, []);
Then, the Redux Store offers a small API surface to interact with it -- e.g. dispatching a Redux Action:
store.dispatch({ type: 'TODO_ADD', todo: { id: '0', name: 'learn redux', completed: false },});
Finally, in JavaScript, you can listen to changes with the Redux Store:
store.subscribe(() => { console.log(store.getState());});
That's Redux in a nutshell with all its fragments: Action, Reducer, Store. There is no React and no View yet. The View could be considered as the console.log
. If you didn't learn Redux yet, feel free to check out this long read React + Redux tutorial which teaches Redux before it integrates into React. Follow for more update at react js Training
Redux's Actions, Reducers, Store have all their mandatory place in the Redux toolchain. If there needs to be syntax sugar on top, one can add Action Creators and Selectors. All you need to get started is the redux library to create the Redux Store. Everything else is just JavaScript.
Also, there is nothing to see about a library like React yet. It's clearly separated with its own library -- react-redux -- and ecosystem.
IMMUTABILITY
Immutability wasn't a big deal within the JavaScript landscape earlier than Redux. acting mutations on variables were all people's usual commercial enterprise. however, with the creation of the cutting-edge frontend frameworks and scaling web applications, many human beings felt the ache of passing round mutable facts. converting a variable at one location meant unexpected facet-consequences at some other location for your application.
In Redux the whole lot within the country field needs to be treated as immutable facts shape -- which isn't enforced even though. if you are adding an entry to an array, with Redux one got used to JavaScript capabilities which treat your facts structures as immutable:
// doconst newState = state.concat(action.todo);// don'tstate.push(action.todo);
There are various array and object functions that return new arrays/objects -- keeping them immutable -- instead of mutating them. Also, recent language additions helped a lot to facilitate this new mindset:
const toggledTodo = { ...todo, completed: !todo.completed };
People started to think about these nuances regarding immutable data structures in JavaScript -- which had superior benefits for the overall JavaScript development experience. No more leaking variables which were mutated at various places in one's application.
I believe Redux has taught us a great deal about separating things into atomic parts. Within the library -- with its Actions, Reducers, and Store -- everything has its purpose and clear API, but also outside of the library with all the bindings for different frameworks like React and Angular.
It gave everyone contributing to the ecosystem the master plan on how things should be done with clear constraints and a simple API.
If you want to learn more about React.js, then The Complete React js Online Training India is a great course, to begin with.