【问题标题】:React Redux State Object propertiesReact Redux 状态对象属性
【发布时间】:2019-06-03 16:46:17
【问题描述】:

我想知道为什么我的状态 todos 在 redux 开发工具中名为 todo 而不是 todos .. 那个名字是从哪里来的? 没有初始状态..我想知道..

我正在关注 Stephen Grider udemy 课程,但使用 todos 而不是流作为修订版

为什么我必须通过state.todo 而不是state.todos 退货??

My github Repo

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


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

tod​​oReducer.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('/');
    };



【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

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

    在这里您可以在沙盒中检查控制台 https://codesandbox.io/s/github/HosMercury/todos

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 2018-07-01
      • 2021-05-28
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多