An intro to Redux (mostly without React)
http://codepen.io/collection/DYWRkW/
https://slides.com/justindragos/redux/live#/
Justin Dragos @EllisandePoet
http://codepen.io/ellisande/pen/amJYVw
Disclaimer: I love Moment
Disclaimer: I love Moment
http://codepen.io/ellisande/pen/ZpZxrL
Disclaimer: I don't hate JS Arrays that much
Disclaimer: I love JavaScript
http://codepen.io/ellisande/pen/VKbwjr
It can help with:
Action
Reducer
New State
Dispatch
Store
Subscribe
const updateUserName = {
type: 'UPDATE_USER_NAME',
newName: 'Justin'
};
//Function that returns an action
const updateUserName = newName => {
return {
type: 'UPDATE_USER_NAME',
newName
};
};
//Create the action, then dispatch to the store
myAppStore.dispatch( updateUserName('Justin') );
const myReducer = (currentState, action) => {
//State can't change, just always return current
return currentState;
}
const myReducer = (currentState, action) => {
//Tell the reducer how to handle appropriate actions
if(action.type === 'UPATE_USER_NAME'){
//The new state
return {
userName: action.newName
};
}
//Return current state if we don't care about the action
return currentState;
}
const myReducer = (currentState, action) => {
const newState = Object.assign({}, currentState);
//Tell the reducer how to handle appropriate actions
if(action.type === 'UPATE_USER_NAME'){
//The new state
newState.userName = newName;
}
//Will be current state if no actions matched
return newState;
}
//Bad because you are mutating current state
const myReducer = (fans, action) => {
if(action.type === 'ADD_FAN'){
fans.push(action.newFan);
}
return fans;
}
//Good because you are not mutating the existing array
const myReducer = (fans, action) => {
if(action.type === 'ADD_FAN'){
return [...fans, action.newFan];
}
return fans;
}
//Dispatch an action to the store
myAppStore.dispatch( myAction );
//Get current state
myAppStore.getState();
//Create the store (one time)
const myAppStore =
redux.createStore( reducer, initialState, middleware );
//Listener is called when state changes
myAppStore.subscribe( listener );
http://codepen.io/ellisande/pen/amJYVw
const myReducer = Redux.combineReducers({
//Reducer for *just* the userName part of state
userName: (currentName, action) => {
//Only get passed the old userName
if(action.type === 'NEW_USER'){
//I just return the new userName
return action.userName;
}
//Just return the new user name
return currentName;
});
Large Reducer:
http://codepen.io/ellisande/pen/pEWxOw
Smaller Reducer:
http://codepen.io/ellisande/pen/ozGaPm
http://codepen.io/ellisande/pen/zKEmya?editors=0011
const logger = store => next => action => {
//Your chance to do something before the action is processed
const updatedAction = next(action);
//Your chance to do something after the action is processed
return updatedAction;
}
Action
Reducer
New State
Dispatch
Store
Subscribe
Props
Component
Events
import {connect} from 'react-redux';
class ShowMe {
render(){
return (<div>Hello from {this.props.userName}!</div>);
}
};
export default connect(i=>i)(ShowMe);
/*
state structure:
{
userName: 'Justin'
}
*/
http://codepen.io/ellisande/pen/WGkYwE
https://github.com/Ellisande/time-travel
UI
UI
Store
Sever
Store 1
Store 2
Store 3
Store 4
Action
Action