state

    1. {
    2. todos: [{
    3. text: 'Eat food',
    4. completed: true
    5. }, {
    6. text: 'Exercise',
    7. completed: false
    8. }],
    9. visibilityFilter: 'SHOW_COMPLETED'
    10. }

    reducer

    1. function todos(state = [], action) {
    2. switch (action.type) {
    3. case 'ADD_TODO':
    4. return state.concat([{ text: action.text, completed: false }])
    5. case 'TOGGLE_TODO':
    6. return state.map((todo, index) =>
    7. action.index === index
    8. ? { text: todo.text, completed: !todo.completed }
    9. : todo
    10. )
    11. default:
    12. return state
    13. }
    14. }

    action

    Actions are plain objects describeing what happened in the app, and serve as the sole way to describe an intention to mutate the data.

    1. { type: 'ADD_TODO', text: 'Use Redux' }

    action creator
    使用函数来创建 action。

    1. export function addTodo(text) {
    2. return {
    3. type: 'ADD_TODO',
    4. text
    5. }
    6. }
    7. dispatch(addTodo('Use Redux'))
    1. function makeActionCreator(type, ...argNames) {
    2. return function (...args) {
    3. const action = { type }
    4. argNames.forEach((arg, index) => {
    5. action[argNames[index]] = args[index]
    6. })
    7. return action
    8. }
    9. }