搜尋

首頁  >  問答  >  主體

類型錯誤:date.locale 不是函數

我使用 antd "antd": "^5.5.1" DatePicker 來綁定日期字串,這是 App.tsx 程式碼如下:

import { DatePicker, Form } from "antd";
import React from "react";
import { useState } from "react";

export interface EduModel {
  admission: String;
}

const App: React.FC = () => {

  const [edu, setEdu] = useState<EduModel>();

  const onFinish = (values: any) => {

  };

  React.useEffect(() => {
    let edu1: EduModel = {
      admission: '1995-09-01'
    };
    setEdu(edu1);
  }, []);

  const onFinishFailed = (errorInfo: any) => {

  };

  const [form] = Form.useForm();

  React.useEffect(() => {
    form.setFieldsValue(edu)
  }, [form, edu]);

  return (
    <div>
      <Form
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        size="large"
        form={form}
      >
        <Form.Item
          label={"admission"}
          name="admission"
          rules={[
            { required: true, message: "please input date" }
          ]}>
          <DatePicker format={"YYYY-MM-DD"}></DatePicker>
        </Form.Item>
      </Form>
    </div>
  );
}

export default App;

當我運行這個簡單的頁面時,顯示錯誤:

dayjs.js:185 Uncaught TypeError: date.locale is not a function
    at Object.format (dayjs.js:185:1)
    at formatValue (dateUtil.js:104:1)
    at useValueTexts.js:18:1
    at useMemo (useMemo.js:5:1)
    at useValueTexts (useValueTexts.js:8:1)
    at InnerPicker (Picker.js:150:1)
    at renderWithHooks (react-dom.development.js:16305:1)
    at updateFunctionComponent (react-dom.development.js:19588:1)
    at beginWork (react-dom.development.js:21601:1)
    at beginWork (react-dom.development.js:27426:1)

我嘗試新增 DatePicker 字串格式,但仍然無法解決此問題。我錯過了什麼嗎?我該怎麼做才能讓它發揮作用?

P粉006847750P粉006847750326 天前700

全部回覆(1)我來回復

  • P粉464208937

    P粉4642089372024-01-09 17:08:41

    date.locale 錯誤 ant datepicker

    我認為 Ant Datepicker 在版本 5 中接受 dayjs 類型或字串,不能接受 undefined。最初您的表單的值是未定義的。稍後更新它的字串。

    你能嘗試嗎?

    範例

    當您設定初始值時:

    form.setFieldsValue({
     admission: dayjs(edu?.admission)
    });

    當您提交值時:

    const onFinish = (values: any) => {
     console.log({
      ...values,
      admission: dayjs(values?.admission).format("YYYY-MM-DD")
     });
    };

    希望能幫到你。

    import { DatePicker, Form, Button } from "antd";
    import React from "react";
    import { useState } from "react";
    import dayjs, { Dayjs } from "dayjs";
    
    export interface EduModel {
      admission: String;
    }
    
    const App: React.FC = () => {
      const [edu, setEdu] = useState<EduModel>();
    
      const onFinish = (values: any) => {
        console.log({
          ...values,
          admission: dayjs(values?.admission).format("YYYY-MM-DD")
        });
      };
    
      React.useEffect(() => {
        let edu1: EduModel = {
          admission: dayjs("1995-09-01").format("YYYY-MM-DD")
        };
        setEdu(edu1);
      }, []);
    
      const onFinishFailed = (errorInfo: any) => {};
    
      const [form] = Form.useForm();
    
      React.useEffect(() => {
        if (edu) {
          form.setFieldsValue({
            admission: dayjs(edu?.admission)
          });
        }
      }, [form, edu]);
    
      return (
        <div>
          <Form
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
            size="large"
            form={form}
          >
            <Form.Item
              label={"admission"}
              name="admission"
              rules={[{ required: true, message: "please input date" }]}
            >
              <DatePicker format={"YYYY-MM-DD"}></DatePicker>
            </Form.Item>
            <Form.Item>
              <Button htmlType="submit" type="primary">
                Submit
              </Button>
            </Form.Item>
          </Form>
        </div>
      );
    };
    
    export default App;

    回覆
    0
  • 取消回覆