24. Redux
… is a predictable state container for JavaScript apps.
Three Principles:
● Single source of truth
● State is read-only
● Changes are made with pure functions
25. Redux
Single source of truth
The state of your whole application is stored in an object tree within a single store
let initialState = {
todos: [ {...}, {...}, … {...} ],
filter: "active"
}
26. Redux
State is read-only
The only way to mutate the state is to emit an action, an object describing what
happened
const action = {
type: SET_VISIBILITY_FILTER,
filter: "active"
}
27. Redux
Changes are made with pure functions
Pure reducers specify the state tree transformations by actions
const todoApp = (state = initialState, action) => {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return {...state, visibilityFilter: action.filter};
default:
return state;
}
}
28. Redux
The Store is the object that brings them together
Create store:
let store = createStore(todoApp, initialState);
Dispatch an action:
store.dispatch(filterAction);
Subscribe/unsubscribe
let unsubscribe = store.subscribe(() =>
console.log(store.getState());
);
33. Links to start with
● Pete Hunt: React How-to
https://github.com/petehunt/react-howto
● Getting started with React
http://facebook.github.io/react/docs/getting-started.html
● Dan Abramov: Getting started with Redux
https://egghead.io/series/getting-started-with-redux
● 0 to 1 : Getting started with Redux
http://www.jchapron.com/2015/08/14/getting-started-with-redux/
https://habrahabr.ru/post/269831/