我正在尝试以我的material-ui形式实现Formik字段。除日期外,我一切正常。什么样的恶梦。我遇到了很多问题,但是当前的一个问题是React告诉我:
尝试导入错误:未从'../formfields'中导出DatePickerField
这是完整的DatePickerField.jsx文件:
import React, { useState, useEffect } from 'react';
import { useField } from 'formik';
import Grid from '@material-ui/core/Grid';
import DateFnsUtils from '@date-io/date-fns';
import {
MuiPickersUtilsProvider,
KeyboardDatePicker
} from '@material-ui/pickers';
const DatePickerField = props => {
const [field, meta, helper] = useField(props);
const { touched, error } = meta;
const { setValue } = helper;
const isError = touched && error && true;
const { value } = field;
const [selectedDate, setSelectedDate] = useState(null);
useEffect(() => {
if (value) {
const date = new Date(value);
setSelectedDate(date);
}
}, [value]);
function _onChange(date) {
if (date) {
setSelectedDate(date);
try {
const ISODateString = date.toISOString();
setValue(ISODateString);
} catch (error) {
setValue(date);
}
} else {
setValue(date);
}
}
return (
<Grid container>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
{...field}
{...props}
variant="inline"
format="MM/dd/yyyy"
value={selectedDate}
onChange={_onChange}
error={isError}
invalidDateMessage={isError && error}
helperText={isError && error}
disableToolbar
KeyboardButtonProps={{
'aria-label': 'change date',
}}
/>
</MuiPickersUtilsProvider>
</Grid>
);
}
export default DatePickerField;
如您所见,我很好地将其导出。但是,当我尝试将其导入到表单中时,出现上述错误。我都尝试过:
import { DatePickerField } from '../../forms/formfields';
并且:
import DatePickerField from '../../forms/formfields';
但是我都会以两种方式得到错误。我究竟做错了什么?任何帮助,不胜感激!
答案 0 :(得分:0)
您没有导入文件。您放置了路径,但没有从formsfields文件夹中添加所需的文件。 因此,将/ DatePickerField附加到导入路径