Redux
?Lib que gerencia uma State
previsível e imutável
Single Source of Truth
State is read-only
Changes are made with pure functions
Uma intenção de mudar o state
. Único caminho para a store
.
let nextVampireId = 0;
export const ADD_VAMPIRE = 'ADD_VAMPIRE';
/* Action Creator */
export function addVampire(vamp) {
/* Action */
return {
type: ADD_VAMPIRE,
payload: {
id: nextVampireId++,
name: vamp
}
};
}
<button onClick={dispatch(addVampire('Tiago'))}>Add Vampire</button>
Altera o state
pelas Actions
import { ADD_VAMPIRE } from './vampires.js';
export default function vampires(state = [], action) {
switch (action.type) {
case ADD_VAMPIRE:
const { id, name } = action.payload;
return [
...state,
{ id, name }
]
default:
return state;
}
}
import { createStore, combineReducers } from 'redux';
import { vampires } from './reducers.js';
let store = createStore(combineReducers({ vampires }));
/* {
vampires: []
} */
store.dispatch(addVampire('Temer'));
/* {
vampires: [{ id: 1, name: 'Temer'}]
} */
Code Time
React Conf: Hot Reloading with Time Travel