Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form.setFieldsValue 之后再去选择日期 出错clone.weekday is not a function #26190

Closed
1 task
KouSum opened this issue Aug 13, 2020 · 27 comments · Fixed by react-component/picker#214
Closed
1 task
Labels
❓FAQ issues would be discussed a lot

Comments

@KouSum
Copy link

KouSum commented Aug 13, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

[https://ant.design/docs/react/replace-moment-cn 按照此教程替换 Moment.js为dayjs ](https://ant.design/docs/react/replace-moment-cn 按照此教程替换 Moment.js为dayjs )

Steps to reproduce

按照教程替换 Moment.js为dayjs form中使用RangePicker form.setFieldsValue 之后再去选择日期 出错clone.weekday is not a function

import React, { useState, useEffect } from 'react';
import {
  Form,
  Button,
} from 'antd';
import { DatePicker, Calendar } from '../components';
import format from 'dayjs';


const dateFormat = 'YYYY-MM-DD';
const { RangePicker } = DatePicker;
function Demo(props) {
  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue({
      dates: [format("2020-05-05",dateFormat), format("2020-05-06",dateFormat)],
    });
  }, []);



  const onFinish = values => {
    console.log('Finish:', values);
  };

  function dateChange(a,b,c){
    console.log(a,b,c)
  }

  return (

    <Form form={form} name="statis" layout="inline" onFinish={onFinish}>
      <Form.Item name="dates">
        <RangePicker initialValues={[format('2015-01-01', dateFormat), format('2015-01-01', dateFormat)]} onChange={dateChange} format={dateFormat} />
      </Form.Item>
      <Form.Item>
        <Button
          type="primary"
          htmlType="submit"
          onClick={() => {
            console.log(
              form.getFieldsValue(),
              format('2018-08-08').format('YYYY-MM-DD'),
            );
          }}
        >
          查询
        </Button>
      </Form.Item>
    </Form>
  );
}

export default Demo;

What is expected?

form.setFieldsValue之后可以正常选择日期

What is actually happening?

出错clone.weekday is not a function

Environment Info
antd 4.5.3
React 16.12.0
System win10
Browser 谷歌浏览器

部分错误提示
dayjs.js:73

Uncaught TypeError: clone.weekday is not a function
at Object.getWeekDay (dayjs.js:73)
at getWeekStartDate (dateUtil.js:97)
at DateBody (DateBody.js:21)
at renderWithHooks (react-dom.development.js:14803)
at mountIndeterminateComponent (react-dom.development.js:17482)
at beginWork (react-dom.development.js:18596)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at beginWork$1 (react-dom.development.js:23203)
at performUnitOfWork (react-dom.development.js:22154)
at workLoopSync (react-dom.development.js:22130)
at performSyncWorkOnRoot (react-dom.development.js:21756)
at react-dom.development.js:11089
at unstable_runWithPriority (scheduler.development.js:653)
at runWithPriority$1 (react-dom.development.js:11039)
at flushSyncCallbackQueueImpl (react-dom.development.js:11084)
at flushSyncCallbackQueue (react-dom.development.js:11072)
at discreteUpdates$1 (react-dom.development.js:21893)
at discreteUpdates (react-dom.development.js:806)
at dispatchDiscreteEvent (react-dom.development.js:4168)

@07akioni
Copy link
Contributor

please provide a codesandbox link

@07akioni 07akioni added the 🤔 Need Reproduce We cannot reproduce your problem label Aug 13, 2020
@ant-design-bot
Copy link
Contributor

Hello @KouSum. Please provide a online reproduction by forking this link https://u.ant.design/codesandbox-repro or a minimal GitHub repository. Issues labeled by Need Reproduce will be closed if no activities in 7 days.

你好 @KouSum, 我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击 此处 创建一个 codesandbox 或者提供一个最小化的 GitHub 仓库。7 天内未跟进的 issue 将会被自动关闭。

@KouSum
Copy link
Author

KouSum commented Aug 14, 2020

重装依赖包后就好了

@KouSum KouSum closed this as completed Aug 14, 2020
@crownclownwl
Copy link

我也遇到这个问题了,回显之后,在编辑就报错

@KouSum
Copy link
Author

KouSum commented Aug 14, 2020

https://codesandbox.io/s/antd-reproduction-template-forked-6qd12?fontsize=14&hidenavigation=1&theme=dark
不知道重现的是否正确,这里是正常的。

@G-WangZH
Copy link

G-WangZH commented Sep 9, 2020

我也遇到这个问题了,回显之后,在编辑就报错

我yongdayjs时也遇到了,请问解决了吗

@victorWuxz
Copy link

我也遇到了同样的问题

@harshithmullapudi
Copy link

4.5.0版本更新打破了dayjs的实现。这似乎适用于4.4.3

@Deboracgs
Copy link

I have the same problem with datepicker, any update?

@crownclownwl
Copy link

把项目中的 day.js 改成 moment.js 完美解决,就是 moment.js 太大了

@fabiendeborde
Copy link

Having the same issue here.
I would be really great if dayjs was better supported and didn't need a "hacky" way to implement it.
(a props passed to the picker would be the easiest way to use it)

@hoangmeo
Copy link

update lastest version dayjs

@Deboracgs
Copy link

@crownclownwl moment.js is not recommended to use now, dayjs is the recommended to use

@Deboracgs
Copy link

@hoangmeo I'm using the lastest version

@rohitsahu4
Copy link

found a way to do this.

import weekday from "dayjs/plugin/weekday"
import localeData from "dayjs/plugin/localeData"

dayjs.extend(weekday)
dayjs.extend(localeData)

I guess DatePicker would use these dayjs plugins so you would have to add these plugins yourself.

@marian2js
Copy link

This is an issue with rc-picker: react-component/picker#123

@JemarJones
Copy link

The required plugins should probably be listed on that "Replace Moment.js" page IMO

@afc163
Copy link
Member

afc163 commented Jan 14, 2021

@iamkun Should we update documentation?

@iamkun
Copy link
Contributor

iamkun commented Jan 14, 2021

@iamkun Should we update documentation?

@afc163 This is because, after 4.5.0, Antd uses moment().weekday which needs an additional Day.js plugin.

I'll update antd-dayjs-webpack-plugin to add the relevant plugin to make it work.

@afc163
Copy link
Member

afc163 commented Jan 14, 2021

https://ant.design/docs/react/replace-moment-cn should include these additional Day.js plugins.

@iamkun
Copy link
Contributor

iamkun commented Jan 14, 2021

Correct, and I'll update rc-picker/lib/generate/dayjs as well

@iamkun
Copy link
Contributor

iamkun commented Jan 17, 2021

After digging this issue, it is actually because there are TWO Day.js package installed in node_modules, one node_modules/dayjs, the other might in node_modules/rc-picker/dayjs, and this is the reason that caused this error.

SOLUTION:

  1. Add dayjs to the dependency to package.json
  2. delete node_modules and package-lock.json
  3. npm install
  4. check and make sure there are only ONE Day.js package installed in node_modules

ref: ant-design/antd-dayjs-webpack-plugin#36

@cjfff
Copy link

cjfff commented Feb 7, 2021

After digging this issue, it is actually because there are TWO Day.js package installed in node_modules, one node_modules/dayjs, the other might in node_modules/rc-picker/dayjs, and this is the reason that caused this error.

SOLUTION:

  1. Add dayjs to the dependency to package.json
  2. delete node_modules and package-lock.json
  3. npm install
  4. check and make sure there are only ONE Day.js package installed in node_modules

ref: ant-design/antd-dayjs-webpack-plugin#36

this solution can resolute my project.

but the dayjs instance maybe not in your node_modules, it could build in another package that it would not the same dayjs and the rc-picker import

@PipBoy2000
Copy link

4. check and make sure there are only ONE Day.js package installed in node_modules

Yeah, that isn't working no matter how many times I nuke my package-lock.json and node_modules. The rc-picker always has that dependency:

  "dependencies": {
    "@babel/runtime": "^7.10.1",
    "classnames": "^2.2.1",
    "date-fns": "^2.15.0",
    "dayjs": "^1.8.30",
    "moment": "^2.24.0",
    "rc-trigger": "^5.0.4",
    "rc-util": "^5.4.0",
    "shallowequal": "^1.1.0"
  }

@iamkun
Copy link
Contributor

iamkun commented Feb 9, 2021

@PipBoy2000 Thanks, fixed in react-component/picker#214

@YemreAybey
Copy link

YemreAybey commented Feb 10, 2021

I believe we will wait until ant-design update rc-picker version right?

P.S: No we don't. Clearing the lock and node_modules and than installing the packages again solved my problem. Thanks everyone who rised and worked on this issue.

@Messiahhh
Copy link
Contributor

After digging this issue, it is actually because there are TWO Day.js package installed in node_modules, one node_modules/dayjs, the other might in node_modules/rc-picker/dayjs, and this is the reason that caused this error.

SOLUTION:

  1. Add dayjs to the dependency to package.json
  2. delete node_modules and package-lock.json
  3. npm install
  4. check and make sure there are only ONE Day.js package installed in node_modules

ref: ant-design/antd-dayjs-webpack-plugin#36

Solved my problem

@afc163 afc163 added ❓FAQ issues would be discussed a lot and removed 🤔 Need Reproduce We cannot reproduce your problem labels Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓FAQ issues would be discussed a lot
Projects
None yet
Development

Successfully merging a pull request may close this issue.