简体   繁体   English

用状态反应对象的redux更新状态

[英]react redux updating state of an object with state

When my component mounts I get from an API and when successful my state looks like this, 当我的组件挂载时,我从API获取信息,当成功时,我的状态如下所示:

    {
        "brief":"No brief given",
        "tasks":[
            {
                "_id":"5c74ffc257a059094cf8f3c2",
                "task":"87 Development Task 7",
                "project":"5c7458abd2fa91567f4e4f03",
                "date_created":"2019-02-26T08:58:42.260Z",
                "__v":0
            }
        ],
        "_id":"5c7458abd2fa91567f4e4f03",
        "owner":{
            "_id":"5c5af553eea087426cd5f9b9",
            "name":"Test test",
            "email":"test@test.com",
            "avatar":"placeholder.jpg",
            "created_at":"2019-02-06T14:55:15.321Z",
            "__v":0
        },
        "name":"87 Development Project 1",
        "status":"Pending",
        "slug":"87-development-project-1",
        "created_at":"2019-02-25T21:05:47.643Z",
        "__v":0
   }

What I am wanting to mark a task as complete, but I am confused over my reducer and action, the flow is the user clicks a button with fires the action, which posts to my api, which responds with an updated task object. 我想将一个任务标记为已完成,但是我对我的化简和动作感到困惑,流程是用户单击一个带有触发动作的按钮,该动作发布到我的api中,并用更新的任务对象进行响应。 However I am confused as to how I go about moving that updated task object into my state as the object is within an array within the state? 但是,我对于如何将更新后的任务对象移入我的状态感到困惑,因为该对象位于状态内的数组中? Can anyone help me out? 谁能帮我吗?

This is my action, 这是我的行动

import axios from "axios";

import { COMPLETE_TASK } from "./types";

// Set logged in user
export const completeTask = id => dispatch => {
  axios.post(`/api/tasks/complete`, { id }).then(res => {
    dispatch({
      type: COMPLETE_TASK,
      payload: res.data
    });
  });
};

And this is my reducer, 这是我的减速器,

import { COMPLETE_TASK } from "../actions/types";

const initialState = { project: { tasks: [], owner: {} } };

export default function(state = initialState, action) {
  switch (action.type) {
    case COMPLETE_TASK:
      return {
        ...state,
        ????
      };
    default:
      return state;
  }
}

Now my feeling is that my initial state will clear any previous state? 现在我的感觉是我的初始状态会清除以前的任何状态? secondly how do I move the returned payload into the correct task object? 其次,如何将返回的有效负载移到正确的任务对象中?

What does the response from your API /api/tasks/complete look like ? 您的API /api/tasks/complete的响应是什么样的? Is your API supposed to complete a task or retrieve a list of tasks ? 您的API是否应该完成任务或检索任务列表?

Anyway, you usually would use Array.prototype.map , Array.prototype.filter and Array.prototype.reduce in your reducers, eg if your API is for marking a task as complete and returns the id of the task that has been properly marked as completed, then your reducer would maybe look like this : 无论如何,您通常会在化简器中使用Array.prototype.mapArray.prototype.filterArray.prototype.reduce ,例如,如果您的API是用于将任务标记为完成并返回已正确标记的任务的ID完成后,减速器可能看起来像这样:

export default function(state = initialState, action) {
  switch (action.type) {
    case COMPLETE_TASK:
      return {
        ...state,
        tasks: state.tasks.filter((task) => {
          return task.id !== action.taskToCompleteId
        })
      };
    default:
      return state;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM