David Khourshid
This guy
function add(a, b) {
return a + b;
}
add(1, 2); // 3
Given the same input, always returns the same output.
function isAngular2ReleasedYet() {
return false;
}
isAngular2ReleasedYet(); // false
Goes hand-in-hand with pure functions.
Stateful: "How many goats do you have right now?"
Stateless: "If you had 3 goats and I gave you a goat because I am a generous goat lover, how many goats do you have?"
Don't be like this guy.
Do one thing at a time.
( , )
Pure function
Immutable state
New state
function addDuck() {
return { type: 'ADD_DUCK' }
}
function removeDuck() {
return { type: 'REMOVE_DUCK' }
}
function duckReducer(state, action) {
switch (action.type) {
case 'ADD_DUCK':
return {
...state,
count: state.count + 1
}
case 'REMOVE_DUCK':
return {
...state,
count: state.count - 1
}
default:
return state;
}
}
const INITIAL_STATE = { count: 0 };
let store = createStore(duckReducer, INITIAL_STATE);
store.dispatch(addDuck());
store.subscribe(() => {
console.log(store.getState());
})
store.dispatch()
store.subscribe()
store.getState()
let store = combineReducers({
duck: duckReducer,
goat: goatReducer
})
compose(f, g)(x) == f(g(x))
{
ducks: 3
}
{
duck: {
ducks: 3
},
goat: {
goats: 4
}
}
unless I ran out of time.
NO REACT!