Created
October 15, 2017 15:46
-
-
Save guangLess/7374e05c3db8a6da7dad3a94d90deaeb to your computer and use it in GitHub Desktop.
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
const todo = (state, action) => { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
completed:false, | |
text: action.text, | |
}; | |
case 'TOGGLE_TODO': | |
if(state.id !== action.id){ | |
return state; | |
} | |
return { | |
...state, | |
completed: !todo.completed | |
} | |
default: | |
return state; | |
} | |
} | |
const todos = (state = [], action) => { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return [ | |
...state, | |
todo(undefined, action) | |
]; | |
case 'TOGGLE_TODO': | |
return state.map(t => todo(t,action)) | |
default: | |
return state; | |
} | |
}; | |
const visibilityFilter = ( | |
state = 'SHOW_ALL', | |
action | |
) => { | |
switch (action.type){ | |
case 'SET_VISIBILITY_FILTER': | |
return action.filter; | |
default: | |
return state; | |
} | |
} | |
const combineReducers = (reduce) => { | |
return (state = {}, action) => { | |
return Object.keys(reducers).reduce( | |
(nextState, key) => { | |
nextState[key] = reducers[key]( | |
state[key], | |
action | |
); | |
return nextState; | |
}, | |
{} | |
) | |
} | |
} | |
//const {combineReducers} = Redux; | |
const todoApp = combineReducers({ | |
todos, // same as todos: todos | |
visibilityFilter | |
}) | |
// const todoApp = (state = {}, action) => { | |
// return { | |
// todos: todos( | |
// state.todos, | |
// action | |
// ), | |
// visibiltyFilter: visibilityFilter ( | |
// state.visibilityFilter, | |
// action | |
// ) | |
// }; | |
// }; | |
const togggleAction = { | |
type: 'TOGGLE_TODO', | |
id:1 | |
} | |
const preState = [{ | |
id:1, | |
completed:false, | |
text: 'Learn Redux' | |
}, | |
{ | |
id:0, | |
completed:false, | |
text: 'Getting it' | |
}] | |
const {createStore} = Redux; | |
const store = createStore(todoApp); | |
console.log("Initial state==",store.getState()); | |
store.dispatch({type:'ADD_TODO', id:4, text:'hello'}) | |
console.log('current state', store.getState()); | |
store.dispatch({type:'ADD_TODO', id:5, text:'sleeppy'}) | |
store.dispatch({type:'SET_VISIBILITY_FILTER', filter: 'SHOW_COMPLETED'}) | |
console.log('current state--->', store.getState()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment