{
todos: [{
text: 'Eat food',
completed: true
}, {
text: 'Workout',
completed: false
}],
visibilityFilter: 'SHOW_COMPLETED'
}
{ type: 'ADD_TODO', text: 'Go for walk' }
{ type: 'TOGGLE_TODO', index: 1 }
{ type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_ALL' }
function visibilityFilter(state = 'SHOW_ALL', action) {
if (action.type === 'SET_VISIBILITY_FILTER') {
return action.filter
} else {
return state
}
}
function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([{ text: action.text, completed: false }])
case 'TOGGLE_TODO':
return state.map(
(todo, index) =>
action.index === index
? { text: todo.text, completed: !todo.completed }
: todo
)
default:
return state
}
}
function todoApp(state = {}, action) {
return {
todos: todos(state.todos, action),
visibilityFilter: visibilityFilter(state.visibilityFilter, action)
}
}
// example declaration of action
const ADD_TODO = 'ADD_TODO'
// example of action being 'dispatched'
{
type: ADD_TODO,
text: 'Build my first Redux app'
}
// example declaration of action
const ADD_TODO = 'ADD_TODO'
// example of action creator
function addTodo(text) {
return {
type: ADD_TODO,
text
}
}
// the result of action creator is passed into dispatch
dispatch(addTodo(text)) // const boundAddTodo = text => dispatch(addTodo(text))
function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [
...state,
{
text: action.text,
completed: false
}
]
default:
return state
}
}
import { createStore } from 'redux'
import todoApp from './reducers'
let store = createStore(todoApp)