React Redux状态对象属性

时间:2019-06-03 16:46:17

标签: javascript reactjs redux react-redux

我想知道为什么我的州待办事项在redux开发工具中命名为 todo 而不是 todos 。 这个名字从哪里来的? 没有初始状态..我想知道..

我正在遵循 Stephen Grider 的课程,但使用待办事项而不是流作为修订

为什么我必须由state.todo而不是state.todos退还?

My github Repo

Jsson服务器db.json文件(api文件)


        {
      "todos": [
        {
          "title": "lorem ipsum ",
          "description": "lorem ipsum",
          "id": 4
        }
      ]
    }

todoReducer.js


        import _ from 'lodash';
    import {
      CREATE_TODO,
      EDIT_TODO,
      FETCH_TODO,
      FETCH_TODOS,
      DELETE_TODO
    } from '../actions/types';

    export default (state = {}, action) => {
      switch (action.type) {
        case FETCH_TODOS:
          return { ...state, ..._.mapKeys(action.payload, 'id') };
        case CREATE_TODO:
        case FETCH_TODO:
        case EDIT_TODO:
          return { ...state, [action.payload.id]: action.payload };
        case DELETE_TODO:
          return _.omit(state, action.payload);

        default:
          return state;
      }
    };

actions / index.js


        import todos from '../apis/todos';
    import history from '../history';
    import {
      SIGN_IN,
      SIGN_OUT,
      CREATE_TODO,
      EDIT_TODO,
      FETCH_TODO,
      FETCH_TODOS,
      DELETE_TODO
    } from './types';

    export const signIn = userId => {
      return { type: SIGN_IN, payload: userId };
    };

    export const signOut = () => {
      return { type: SIGN_OUT };
    };

    export const fetchTodos = () => async dispatch => {
      const response = await todos.get('/todos');

      dispatch({ type: FETCH_TODOS, payload: response.data });
    };

    export const createTodo = formValues => async dispatch => {
      const response = await todos.post('/todos', formValues);
      dispatch({ type: CREATE_TODO, payload: response.data });
      history.push('/');
    };



1 个答案:

答案 0 :(得分:2)

https://github.com/HosMercury/todos/blob/master/src/reducers/index.js在这里,您将列表作为待办事项而不是待办事项。

您可以在此处检查沙箱中的控制台 https://codesandbox.io/s/github/HosMercury/todos