Simple Redux
A minimal example of Redux
by Rich Costello
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"></script>
JavaScript
const userReducer = (state={}, action) => {
switch(action.type) {
case "CHANGE_NAME": {
state = {...state, name: action.payload}
break;
}
case "CHANGE_AGE": {
state = {...state, age: action.payload}
break;
}
}
return state;
};
const tweetsReducer = (state=[], actions) => {
return state;
};
const reducers = Redux.combineReducers({
user: userReducer,
tweets: tweetsReducer,
})
const store = Redux.createStore(reducers);
store.subscribe(() => {
console.log("store changed", store.getState())
})
store.dispatch({type: "CHANGE_NAME", payload: "Will"})
store.dispatch({type: "CHANGE_AGE", payload: 35})
store.dispatch({type: "CHANGE_AGE", payload: 36})