Bob Bijvoet
Front-end Chapter Lead at ING
Via FrontMen
A predictable state container for JavaScript apps.
The state of your whole application is stored in an object tree within a single store.
The only way to mutate the state is to emit an action, an object describing what happened.
Changes are made with pure functions
To specify how the state tree is transformed by actions, you write pure reducers.
Pure function
The place where all your application state lives
UI
//Cart
{
products:[],
totalItems:0
}
Tells store that something happened and that the store should update itself in response.
UI
//Add
{
type:'ADD_ITEM',
payload:{
id:1,
name:'Book'
}
}
//Remove
{
type:'REMOVE_ITEM',
payload:{
id:1
}
}
Responsible for mutating the store state when actions are dispatched
UI
reducer(state, action) {
switch (action.type) {
case 'ADD_ITEM':
var cart = [...state.cart];
cart.push(action.payload)
return Object.assign({}, state, {cart});
break;
case 'REMOVE_ITEM':
...
return Object.assign({}, state);
}
}
Provides the ability to create functions that extend the library.
UI
</>
Questions?