Mantine日期选择器引发了一个”对象不能作为React子元素”的错误。

huangapple go评论107阅读模式
英文:

Mantine date picker throws an objects are not valid as a react child error

问题

我正在尝试在NextJS中使用mantine Date。当用户点击日期时,该日期应该显示在HTML文本标题中。例如,如果用户选择了2023年1月1日这个日期,文本应该显示为Date: Sun Jan 01 2023...。我正在使用console.log进行测试,它能够正常工作。但是,当我尝试在文本标题中使用它时,会抛出以下错误:Error: 对象无法用作React子元素(找到:[object Date])。如果您想要渲染一组子元素,请改用数组。

我尝试过对它进行映射,但没有成功。然后我尝试使用const date = [...calendarVal],但它显示TypeError:calendarVal不可迭代

以下是您提供的代码部分,我已经将其翻译好:

  1. import { useState, useEffect } from 'react';
  2. import { Modal, Button, Group } from '@mantine/core';
  3. import { Calendar, RangeCalendar } from '@mantine/dates';
  4. import React from 'react';
  5. export default function Demo() {
  6. const [opened, setOpened] = useState(false);
  7. const [calendarVal, setCalendarVal] = useState(new Date());
  8. const [hydrated, setHydrated] = React.useState(false);
  9. React.useEffect(() => {
  10. setHydrated(true);
  11. }, []);
  12. useEffect(() => {
  13. console.log(calendarVal);
  14. });
  15. if (!hydrated) {
  16. // 在第一次渲染时返回null,以便客户端和服务器匹配
  17. return null;
  18. }
  19. return (
  20. <>
  21. <Modal
  22. opened={opened}
  23. onClose={() => setOpened(false)}
  24. title="Introduce yourself!"
  25. >
  26. {/* 模态框内容 */}
  27. </Modal>
  28. <Group position="center">
  29. <Button onClick={() => setOpened(true)}>Open Modal</Button>
  30. </Group>
  31. <p>Date: {calendarVal}</p>
  32. <Group position='center'>
  33. <Calendar onChange={setCalendarVal} value={calendarVal} />
  34. </Group>
  35. </>
  36. );
  37. }

请注意,我已将useState的初始值更改为new Date(),以确保它是一个Date对象。如果您需要进一步的帮助或有其他问题,请随时提问。

英文:

I'm trying to use mantine Date in NextJS. When a user clicks on the date, that date suppose to display the date in the html text heading. For example, if a user picks the date jan 1st 2023, the text is supposed to look like Date: Sun Jan 01 2023... I'm testing it out using a console.log and it works. However, when I try to use it in the text heading this error gets thrown: Error: Objects are not valid as a React child (found: [object Date]). If you meant to render a collection of children, use an array instead.

I tried adding a map to it that didn't work. Then I tried using const date = [...calendarVal] but it said TypeError: calendarVal is not iterable

  1. import { useState, useEffect } from &#39;react&#39;;
  2. import { Modal, Button, Group } from &#39;@mantine/core&#39;;
  3. import { Calendar, RangeCalendar } from &#39;@mantine/dates&#39;;
  4. import React from &#39;react&#39;;
  5. export default function Demo() {
  6. const [opened, setOpened] = useState(false);
  7. const [calendarVal, setCalendarVal] = useState(Date());
  8. const [hydrated, setHydrated] = React.useState(false);
  9. React.useEffect(() =&gt; {
  10. setHydrated(true);
  11. }, []);
  12. useEffect(() =&gt; {
  13. console.log(calendarVal)
  14. })
  15. if (!hydrated) {
  16. // Returns null on first render, so the client and server match
  17. return null;
  18. }
  19. return (
  20. &lt;&gt;
  21. &lt;Modal
  22. opened={opened}
  23. onClose={() =&gt; setOpened(false)}
  24. title=&quot;Introduce yourself!&quot;
  25. &gt;
  26. {/* Modal content */}
  27. &lt;/Modal&gt;
  28. &lt;Group position=&quot;center&quot;&gt;
  29. &lt;Button onClick={() =&gt; setOpened(true)}&gt;Open Modal&lt;/Button&gt;
  30. &lt;/Group&gt;
  31. &lt;p&gt;Date: {calendarVal}&lt;/p&gt;
  32. &lt;Group position=&#39;center&#39;&gt;
  33. &lt;Calendar onChange={setCalendarVal} value={calendarVal} /&gt;
  34. &lt;/Group&gt;
  35. &lt;/&gt;
  36. );
  37. }

答案1

得分: 1

你不能直接在React中渲染对象,首先需要将它们转换为字符串(通常)。例如,你可以编写以下代码:

  1. &lt;p&gt;日期:{calendarVal.toLocaleString()}&lt;/p&gt;
英文:

You can't render objects directly in React, you first need to convert them to strings (typically). For instance you can write the following:

  1. &lt;p&gt;Date: {calendarVal.toLocaleString()}&lt;/p&gt;

huangapple
  • 本文由 发表于 2023年1月8日 23:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定