搜索

首页  >  问答  >  正文

MUI DatePicker与Formik不兼容:date.isBefore不可用

<p>我正在使用Formik在React中创建一个表单,同时也在使用MUI组件。问题是我遇到了以下错误:</p> <pre class="brush:php;toolbar:false;">date.isBefore不是一个函数 TypeError: date.isBefore不是一个函数 at DayjsUtils.isBeforeDay (http://localhost:3000/static/js/bundle.js:2319:19) at validateDate (http://localhost:3000/main.aa62b4b6c891ec5ff2b0.hot-update.js:10596:43) at useValidation (http://localhost:3000/main.aa62b4b6c891ec5ff2b0.hot-update.js:10654:27) at usePickerValue (http://localhost:3000/main.aa62b4b6c891ec5ff2b0.hot-update.js:10019:75) at usePicker (http://localhost:3000/main.aa62b4b6c891ec5ff2b0.hot-update.js:9876:94) at useDesktopPicker (http://localhost:3000/main.aa62b4b6c891ec5ff2b0.hot-update.js:7848:60) at DesktopDatePicker (http://localhost:3000/main.aa62b4b6c891ec5ff2b0.hot-update.js:3956:90) at renderWithHooks (http://localhost:3000/static/js/bundle.js:103306:22) at updateForwardRef (http://localhost:3000/static/js/bundle.js:105877:24) at beginWork (http://localhost:3000/static/js/bundle.js:107924:20)</pre> <p>这是我的代码:</p> <pre class="brush:php;toolbar:false;"><Formik onSubmit={handleFormSubmit} initialValues={initialValuesProject} validationSchema={projectSchema} > {({ values, handleChange, handleSubmit, setFieldValue }) => ( <form onSubmit={handleSubmit}> <Box width="50%"> <LocalizationProvider dateAdapter={AdapterDayjs}> <DatePicker id="project_start" name="project_start" value={values.project_start} slotProps={{ textField: { size: "small", margin: "dense", }, }} /> <DatePicker id="project_end" name="project_end" value={values.project_end} slotProps={{ textField: { size: "small", margin: "dense" }, }} /> </LocalizationProvider> </Box> </form> )}</Formik></pre> <p>这是模式和初始值:</p> <pre class="brush:php;toolbar:false;">const projectSchema = yup.object().shape({ project_start: yup.date(), project_end: yup.date(), project_name: yup.string().required("required"), usersId: yup.string(), partnerId: yup.string(), categoryId: yup.string(), }); const initialValuesProject = { project_start: Date.now(), project_end: Date.now(), project_name: "", usersId: "", partnerId: "", categoryId: "", };</pre> <p>非常感谢能帮助我的任何人 <3</p> <p>能够使日期选择器工作</p>
P粉046387133P粉046387133466 天前697

全部回复(1)我来回复

  • P粉449281068

    P粉4492810682023-08-26 00:12:31

    看起来问题出在这里:

    project_start: Date.now(),
    project_end: Date.now(),

    project_start和project_end的类型应该是Dayjs:

    import dayjs, { Dayjs } from 'dayjs';
    
    const initialValuesProject = {
      project_start: dayjs(Date.now()),
      project_end: dayjs(Date.now()),
      project_name: "",
      usersId: "",
      partnerId: "",
      categoryId: "",
    };

    回复
    0
  • 取消回复