A CodeRoad tutorial for learning Redux.
CodeRoad is an open-sourced interactive tutorial platform for the Atom Editor. Learn more at CodeRoad.io.
-
install the tutorial package
npm install --save coderoad-redux-js
-
install and run the atom-coderoad plugin
Getting a project setup is rarely easy. Luckily, we have a quick script that can do the work for us.
Running > npm run setup
will do the following:
- Install package dev dependencies
- Create an output directory called "dist"
- Install "concurrently" & "browser-sync" globally
- Run our app in the browser
You'll find this "setup" script located in your package.json.
We'll be installing a lot of scripts from terminal. You may also want to consider installing the atom package "platformio-ide-terminal", which provides a terminal inside your editor.
The "single source of truth".
const store = createStore(reducer, initialState);
Events that change the data.
const action = { type: 'ACTION_NAME' };
const actionName = () => ({ type: 'ACTION_NAME' });
const ACTION_NAME = 'ACTION_NAME'
The data transformation
const reducer = (state) => {
console.log(state);
return state;
};
Reducers must be pure functions
State is "read only".
Notes
const nextPokemon = state.pokemon.map(p => {
if (id === p.id) {
p.votes += 1;
}
return p;
});
return {
pokemon: nextPokemon
};
Create modular, composable reducers with combineReducers
.
Explanation here.
Refactor your project into different files.
Explanation here
The power of middleware with "redux-logger".
Explanation here.
Creating a "SORT_BY_POPULARITY" action.
function sortByVotes(a, b) {
switch(true) {
case a.votes < b.votes: return 1;
case a.votes > b.votes: return -1;
default: return 0;
}
}
Sort pokemon by votes
Using thunks for async actions.