小编Hol*_*515的帖子

复制 React State 对象;修改复制的对象会改变状态对象

我正在尝试复制一个状态对象:

@boundMethod
private _onClickDeleteAttachment(attachmentName: string): void {
    console.log("_onClickDeleteAttachment | this.state.requestApproval[strings.Attachments]: ", this.state.requestApproval[strings.Attachments]);

    let requestApprovalClone = {... this.state.requestApproval}

    if (requestApprovalClone === this.state.requestApproval) {
        console.log("they are ===");
    }
    else {
        console.log(" they are not ===");
    }

    _.remove(requestApprovalClone[strings.Attachments], (attachment: any) => {
        return attachment.FileName === attachmentName;
    })

    console.log("_onClickDeleteAttachment | this.state.requestApproval[strings.Attachments]: ", this.state.requestApproval[strings.Attachments]);
    console.log("_onClickDeleteAttachment | requestApprovalClone[strings.Attachments]: ", requestApprovalClone[strings.Attachments]);
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

state对象被修改过。从我读过的内容来看,我不应该改变一个state对象,而只能用setState.

我该如何纠正?

javascript reactjs

5
推荐指数
2
解决办法
5324
查看次数

在React中循环遍历地图

我有一个Map对象:

let dateJobMap = new Map();

for (let jobInArray of this.state.jobs) {
  let deliveryDate: Date = new Date(jobInArray.DeliveryDate);
  let deliveryDateString: string = deliveryDate.toLocaleDateString("en-US");

  if (dateJobMap.has(deliveryDateString)) {
    let jobsForDate: IDeliveryJob[] = dateJobMap.get(deliveryDateString);
    jobsForDate.push(jobInArray);
  }
  else {
    let jobsForDate: IDeliveryJob[] = [jobInArray];
    dateJobMap.set(deliveryDateString, jobsForDate);
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的render方法中,我想为值数组中的每个传递作业调用TruckJobComp对象以显示它:

        <div className={ styles.column }>
          <p className={ styles.description }>{escape(this.props.description)}</p>

          {
            dateJobMap.forEach(function(jobsForDate, dateString) {
              jobsForDate.map(job => (
                <TruckJobComp job = { job } />
              ))
            })
          }
        </div>
Run Code Online (Sandbox Code Playgroud)

这似乎应该起作用,但不起作用。它从不创建TruckJobComp。我做了.forEach我的迭代Map,并为每个 …

javascript typescript reactjs

4
推荐指数
1
解决办法
4520
查看次数

创建全天活动失败

我正在尝试创建一个全天活动:

let foobar: any = {
    "subject": calendarEvent.Title+"v5",
    "body": {
        "contentType": "HTML",
        "content": calendarEvent! || !calendarEvent.Description ? "No Description": calendarEvent.Description,
    },
    "start": {
        "dateTime": calendarEvent.EventDate,
        "timeZone": moment.tz.guess(),
    },
    "end": {
        "dateTime": calendarEvent.EndDate,
        "timeZone": moment.tz.guess(),
    },
    "location": {
        "displayName": !calendarEvent || !calendarEvent.Location ? "No Location": calendarEvent.Location,
    },
    "isAllDay": !calendarEvent || !calendarEvent.fAllDayEvent ? false : true,
};

context.msGraphClientFactory.getClient()
    .then((client: MSGraphClient) => {
        client.api("/me/calendar/events").post(foobar)
        .then((content: any) => {
            console.log("CalendarService | createCalendarEvent | content: ", content);
        });
    });
Run Code Online (Sandbox Code Playgroud)

日志:

在此输入图像描述

当我包含“isAllDay”属性时,它会失败并返回 400(错误请求)。

我排除了该属性,它正在创建没有问题的事件。

有什么建议么?


编辑: …

typescript spfx microsoft-graph-api

3
推荐指数
1
解决办法
821
查看次数

返回前一天的时刻

我有个约会:

EventDate: "2018-10-10T00:00:00Z"
Run Code Online (Sandbox Code Playgroud)

我运行它moment

let date = moment(event.EventDate).format("MM/DD/YYYY");
Run Code Online (Sandbox Code Playgroud)

我得到前一天的信息:

date:  10/09/2018
Run Code Online (Sandbox Code Playgroud)

怎么了?

编辑:

这不是重复的;有一个具体的答案moment解决了时区问题。

javascript momentjs

1
推荐指数
1
解决办法
1671
查看次数