简体   繁体   English

如何仅在 React 中的特定日期显示提醒

[英]How to show reminder only on specific date in React

In a component, I have a Calendar using react-calendar on the left side.在一个组件中,我在左侧有一个使用 react-calendar 的日历。 I can add reminders via a form where I can select a date.我可以通过表格添加提醒,我可以在其中 select 约会。 These two dates are different displayed: the date from the react-calender is shown in the format 18/08/2021 while the date from the reminder form is shown as 2021-08-18.这两个日期显示不同:react-calender 中的日期显示格式为 18/08/2021,而提醒表单中的日期显示为 2021-08-18。

How can I compare these two values so that only reminders are shown on the specific date when clicking on a calendar date?我如何比较这两个值,以便在单击日历日期时仅在特定日期显示提醒? I tried to parse (which seems to be not recommended by MDN) but the output numbers are different.我尝试解析(MDN 似乎不推荐)但是 output 数字不同。 I tried to compare with valueOf() but the output is false.我试图与 valueOf() 进行比较,但 output 是错误的。

EDIT:编辑:

I was able to compare both dates and the output is true.我能够比较这两个日期,output 是真的。 Therefore, I set a new prop to reminder "show" with the state of "false".因此,我设置了一个新的道具来提醒“显示”与“假”的state。 But when I try to return this prop as true when both dates are the same, the prop does not change.但是当我尝试在两个日期相同时将此道具返回为真时,道具不会改变。 (changing it manually with dev tools, the show/hide function works now perfectly fine). (使用开发工具手动更改它,显示/隐藏 function 现在可以正常工作)。

In my index.js file, I created an onChange function when clicking a specific date in the calendar:在我的 index.js 文件中,我在单击日历中的特定日期时创建了一个 onChange function:

const onCalendarChange = (date) => {
    setDate(date);
    reminders.map((item) => {
      const itemDate = item.date
        .toLocaleString()
        .slice(0, 10)
        .replace(/\D/g, "");
      const calDate = date.toLocaleDateString().split("/").reverse().join("");
      console.log(itemDate === calDate);
      if (itemDate === calDate) {
        return {
          ...item,
          show: true,
        };
      }
      return item;
    });
  };

I also tried it with the filter() but this does not seem to work either.我也用 filter() 尝试过,但这似乎也不起作用。 The reminders are shown the whole time.提醒会一直显示。

 const onCalendarChange = (date) => {
    setDate(date);
    setReminders(
      reminders.filter(
        (item) =>
          item.date.toLocaleString().slice(0, 10).replace(/\D/g, "") ===
          date.toLocaleDateString().split("/").reverse().join("")
      )
    );
  };

1. First code snippet: 1. 第一个代码片段:

The first code snippet may not be working because you forgot to store the new array in a variable and reset reminders on the state using it (which you did correctly in the second code snippet).第一个代码片段可能无法正常工作,因为您忘记将新数组存储在变量中并使用它重置 state 上的reminders (您在第二个代码片段中做对了)。

So, the first code snippet should be something like this:所以,第一个代码片段应该是这样的:

const updatedReminders = reminders.map((item) => {
      // logic here
});

setReminders(updatedReminders);

2. Second Code Snippet: 2. 第二个代码片段:

Not sure why this isn't working, YET.不确定为什么这不起作用,但是。 I'll let you know when I have an update.当我有更新时,我会通知你。

In the meantime, see if my suggestion for the first code snippet does the trick?同时,看看我对第一个代码片段的建议是否有效?

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

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