Jesús Quintana
@marduke182
@marduke182
Barcelona
Digital Origin
Stateful components (this.state)
Services / Factories / Providers / $scope
Ember Data or Services
Backbone Models
These solutions don't scale well
Bugs are hard to replicate (non deterministic)
Tied to frameworks (Hard to migrate)
Prone to errors
let initialState = {
peanut: {
skin: 'white',
hair: 'black',
eyes: 'normal',
glasses: 'white',
mouth: 'big',
shirt: 'yelow_black_jacket',
pants: 'black_jeans',
shoes: 'black_white'
},
setting: {
place: "outside",
time: "9:00am",
season:"SUMMER"
}
};const winterIsComing = {
type: 'CHANGE_SEASON',
season: 'WINTER'
};
const goToSchool = {
type: 'CHANGE_PLACE',
place: 'school'
};
const incrementByAction = {
type: 'CHANGE_HAIR',
colour: 'green'
};winterIsComing
goToSchool
function setting(state, action) {
if (typeof state === 'undefined') {
return settingInitialState;
}
switch (action.type) {
case 'CHANGE_SEASON':
return Object.assign({}, state, {season: action.season});
default:
return state
}
}winterIsComing
The reducer function
{
...
settings: {
...
season: 'SUMMER'
}
}{
...
settings: {
...
season: 'winter'
}
}
{
type: 'CHANGE_SEASON',
season: 'WINTER'
}Lets talk about DX
Middlewares in redux work in a similar fashion than in express. It is something that goes between dispatching an action, and the moment it's handled by the reducer
winterIsComing
Middlewares
Dispatcher
import { createStore, applyMiddleware } from 'redux';
import reducer from './myReducer';
import thunk from 'redux-thunk';
const initialState = { counter: 0 };
const store = createStore(reducer, initialState, applyMiddleware(thunk));
store.subscribe(() => {
// Here i could update my javascript application.
console.log(store.getState());
})
store.dispatch({ type: 'INCREMENT'});
// Must log { counter: 1 }