Viktor Shevchenko
Web developer from Kyiv
Controller-views - listens for events that are broadcast by the stores and exucutes setState() on them
Common
Different
console.log(store.getState())
/* Prints
{
selectedActorId: 'sylvester',
actors: [
{
id: 'sylvester',
fullName: 'Sylvester Stallone',
films: []
},
{
id: 'arnold',
fullName: 'Arnold Schwarzenegger'
films: []
}
]
}
*/
The only way to mutate the state is to emit an action, an object describing what happened.
store.dispatch({
type: 'CHANGE_SELECTED_ACTOR',
id: 'arnold'
})
store.dispatch({
type: 'ADD_FILM',
actorId: 'arnold'
filmName: 'Total Recall'
})
To specify how the state tree is transformed by actions, you write pure reducers.
function film(state = [], action) {
switch(action.type)
case: ADD_FILM
return [...state, action.filmName];
default:
return state;
}
Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. You send them to the store using store.dispatch().
const ADD_FILM = 'ADD_FILM';
{
type: ADD_FILM,
filmName: 'Total Recall'
}
store.dispatch({
type: ADD_FILM,
filmName: 'Total Recall'
})
Action creators are exactly that—functions that create actions.
const ADD_FILM = 'ADD_FILM';
function addFilm(name) {
return {
type: ADD_FILM,
filmName: 'Total Recall'
};
}
store.dispatch(addFilm('Total Recal'))
The reducer is a pure function that takes the previous state and an action, and returns the next state.
{
selectedActorId: 'arnold',
actors: [
{
id: 'sylvester',
fullName: 'Sylvester Stallone',
films: []
},
{
id: 'arnold',
fullName: 'Arnold Schwarzenegger'
films: []
}
]
}
function films (state = [], action) {
switch (action.type)
case ADD_FILM
return [...state, action.filmName]
default
return state
}
Single object that is a container for all application state
By Viktor Shevchenko