Hồ *_*iến 6 reactjs antd react-hook-form dayjs
我将Ant Design版本从4升级到5,并将Moment替换为Day.js,然后遇到了这个错误。
const {
field,
fieldState: { error },
} = useController({ name: name, control: control });
return (
<div>
<RangePicker
style={error ? errorStyle : defaultStyle}
format={customFormatList}
suffixIcon={<BsCalendarDate className="text-lg" />}
value={field.value}
onChange={(e) => {
field.onChange(e);
}}
onBlur={() => {
field.onBlur();
}}
onCalendarChange={(e) => {
field.onChange(e);
}}
{...rest}
/>
{error && <p className="text-sm text-[#ff4d4f]">{error.message}</p>}
</div>
);
Run Code Online (Sandbox Code Playgroud)
然后,我使用react-hook-form设置默认值:
const { control, handleSubmit, setValue, watch } = useForm<SearchFormInputValues>({
defaultValues: {
requestDate: [dayjs(), dayjs()],
},
resolver: yupResolver(schema),
});
...
<DateRangePicker
name="requestDate"
control={control}
placeholder={["dd-mm-yyyy", "dd-mm-yyyy"]}/>
Run Code Online (Sandbox Code Playgroud)
它显示了我需要的正确日期,但是当我单击编辑时,上面出现错误。当我点击旁边的删除按钮并再次选择日期时,就不会出现错误了。
我尝试通过添加以下代码行来修复此错误,但它不起作用:
import dayjs from "dayjs";
import weekday from "dayjs/plugin/weekday";
import localeData from "dayjs/plugin/localeData";
dayjs.extend(weekday);
dayjs.extend(localeData);
dayjs.locale("en");
dayjs.locale("vi");
Run Code Online (Sandbox Code Playgroud)
小智 10
您必须将此代码添加到您的索引应用程序文件中
import dayjs from 'dayjs'
import advancedFormat from 'dayjs/plugin/advancedFormat'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import localeData from 'dayjs/plugin/localeData'
import weekday from 'dayjs/plugin/weekday'
import weekOfYear from 'dayjs/plugin/weekOfYear'
import weekYear from 'dayjs/plugin/weekYear'
dayjs.extend(customParseFormat)
dayjs.extend(advancedFormat)
dayjs.extend(weekday)
dayjs.extend(localeData)
dayjs.extend(weekOfYear)
dayjs.extend(weekYear)
Run Code Online (Sandbox Code Playgroud)
小智 0
import * as dayjs from 'dayjs'
import * as isLeapYear from 'dayjs/plugin/isLeapYear' // import plugin
import 'dayjs/locale/zh-cn' // import locale
dayjs.extend(isLeapYear) // use plugin
dayjs.locale('zh-cn') // use locale
Run Code Online (Sandbox Code Playgroud)
我认为这对你有帮助你可以将代码添加到根应用程序的文件index.tsx或index.js中吗
| 归档时间: |
|
| 查看次数: |
3465 次 |
| 最近记录: |