Created
April 23, 2019 10:29
-
-
Save hjzheng/51ce00986de8ce7caf98b00eee788112 to your computer and use it in GitHub Desktop.
redux createStore and combineReducer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Open the console | |
* to see the state log. | |
*/ | |
const todo = (state, action) => { | |
if (action.type === 'ADD_TODO') { | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
} | |
if (action.type === 'TOGGLE_TODO') { | |
if(state.id === action.id) { | |
return { | |
...state, | |
completed: !state.completed | |
} | |
} else { | |
return state; | |
} | |
} | |
return state; | |
} | |
const todos = (state = [], action) => { | |
if (action.type === 'ADD_TODO') { | |
return [ | |
...state, | |
todo(undefined, action) | |
] | |
} | |
if (action.type === 'TOGGLE_TODO') { | |
return state.map(t => todo(t, action)); | |
} | |
return state; | |
} | |
const visibilityFilter = (state = 'SHOW_ALL', action) => { | |
if (action.type === 'SET_VISIBILITY_FILTER') { | |
return action.filter; | |
} | |
return state; | |
} | |
const createStore = (reducer) => { | |
let state; | |
let listeners = []; | |
const getState = () => state; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach(listener => listener()); | |
} | |
const subscribe = (listener) => { | |
listeners.push(listener); | |
return () => { | |
listeners.filter(l => l !== listener); | |
} | |
} | |
return {getState, dispatch, subscribe} | |
} | |
const combineReducers = (reducers) => { | |
return (state={}, action) => { | |
return Object.keys(reducers).reduce((nextState, key) => { | |
nextState[key] = reducers[key](state[key], action); | |
return nextState; | |
}, {}) | |
} | |
} | |
const todoApp = combineReducers({ | |
todos, | |
visibilityFilter | |
}) | |
const store = createStore(todoApp); | |
console.log('Initial state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO.'); | |
store.dispatch({ | |
type: 'ADD_TODO', | |
id: 0, | |
text: 'Learn Redux' | |
}); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO.'); | |
store.dispatch({ | |
type: 'ADD_TODO', | |
id: 1, | |
text: 'Go shopping' | |
}); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching TOGGLE_TODO.'); | |
store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: 0 | |
}); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching SET_VISIBILITY_FILTER'); | |
store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: 'SHOW_COMPLETED' | |
}); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment