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

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

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不可迭代

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

import { useState, useEffect } from 'react';
import { Modal, Button, Group } from '@mantine/core';
import { Calendar, RangeCalendar } from '@mantine/dates';
import React from 'react';

export default function Demo() {
  const [opened, setOpened] = useState(false);
  const [calendarVal, setCalendarVal] = useState(new Date());
  const [hydrated, setHydrated] = React.useState(false);

  React.useEffect(() => {
    setHydrated(true);
  }, []);

  useEffect(() => {
    console.log(calendarVal);
  });

  if (!hydrated) {
    // 在第一次渲染时返回null,以便客户端和服务器匹配
    return null;
  }

  return (
    <>
      <Modal
        opened={opened}
        onClose={() => setOpened(false)}
        title="Introduce yourself!"
      >
        {/* 模态框内容 */}
      </Modal>

      <Group position="center">
        <Button onClick={() => setOpened(true)}>Open Modal</Button>
      </Group>

      <p>Date: {calendarVal}</p>
      <Group position='center'>
        <Calendar onChange={setCalendarVal} value={calendarVal}  />
      </Group>
    </>
  );
}

请注意,我已将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

import { useState, useEffect } from &#39;react&#39;;
import { Modal, Button, Group } from &#39;@mantine/core&#39;;
import { Calendar, RangeCalendar } from &#39;@mantine/dates&#39;;
import React  from &#39;react&#39;;

export default function Demo() {
  const [opened, setOpened] = useState(false);
  const [calendarVal, setCalendarVal] = useState(Date());
  const [hydrated, setHydrated] = React.useState(false);

  React.useEffect(() =&gt; {
    setHydrated(true);
  }, []);

  useEffect(() =&gt; {
    console.log(calendarVal)
  })

  if (!hydrated) {
    // Returns null on first render, so the client and server match
    return null;
  }
  
  return (
    &lt;&gt;
      &lt;Modal
        opened={opened}
        onClose={() =&gt; setOpened(false)}
        title=&quot;Introduce yourself!&quot;
      &gt;
        {/* Modal content */}
      &lt;/Modal&gt;

      &lt;Group position=&quot;center&quot;&gt;
        &lt;Button onClick={() =&gt; setOpened(true)}&gt;Open Modal&lt;/Button&gt;
        
      &lt;/Group&gt;
      
      &lt;p&gt;Date: {calendarVal}&lt;/p&gt;
      &lt;Group position=&#39;center&#39;&gt;
        &lt;Calendar onChange={setCalendarVal} value={calendarVal}  /&gt;
      &lt;/Group&gt;

    &lt;/&gt;
  );
}

答案1

得分: 1

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

&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:

&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:

确定