reactjs - React-widgets DateTimePicker 无法正常工作,给出无法分配给类型“只读”的错误'
问题描述
嗨,我是 TypeScript 的新手。我无法理解这个错误。如果有人可以提供帮助,那将不胜感激。我试图用谷歌搜索这个问题并试图改变它的版本,但不确定是什么导致了这个错误。我在我semantic-ui-react
的 with中使用这个日期选择器react-final-form
。DateTimePicker
我的 DateInput 组件在我的导入上给了我一个问题。react-final form
对于其他输入效果很好。只是这个问题react widgets
-> DateTimePicker
。
这是错误
TypeScript error in C:/Users/brahm/source/repos/FullStack/SOCIALMEDIA-react-asp.netcore/Reactivities/client-app/src/app/common/form/DateInput.tsx(22,8):
Type '{ readOnly: any; as?: any; children?: ReactNode; className?: string | undefined; content?: ReactNode; control?: any; disabled?: boolean | undefined; error?: SemanticShorthandItem<LabelProps>; ... 9 more ...; time: any; }' is not assignable to type 'Readonly<DateTimePickerProps>'.
Types of property 'id' are incompatible.
Type 'string | number | undefined' is not assignable to type 'string | undefined'.
Type 'number' is not assignable to type 'string | undefined'. TS2322
这是我的 DateInput 组件
import { FieldRenderProps } from 'react-final-form';
import { FormFieldProps, Form, Label } from 'semantic-ui-react';
import { DateTimePicker } from 'react-widgets';
interface IProps
extends FieldRenderProps<Date, HTMLInputElement>,
FormFieldProps {}
const DateInput: React.FC<IProps> = ({
input,
width,
date = false,
time = false,[![enter image description here][1]][1]
placeholder,
meta: { touched, error },
...rest
}) => {
return (
<Form.Field error={touched && !!error} width={width}>
<DateTimePicker
placeholder={placeholder}
value={input.value || null}
onChange={input.onChange}
date={date}
time={time}
{...rest}
/>
{touched && error && (
<Label basic color="red">
{error}
</Label>
)}
</Form.Field>
);
};
export default DateInput;
这是它得到渲染的地方
<Grid>
<Grid.Column width={10}>
<Segment clearing>
<FinalForm
onSubmit={handleFinalFormSubmit}
render={({ handleSubmit }) => (
<Form onSubmit={handleSubmit}>
<Field
name="title"
placeholder="Title"
value={activity.title}
component={TextInput}
/>
<Field
name="discription"
placeholder="Description"
rows={3}
value={activity.discription}
component={TextAreaInput}
/>
<Field
name="category"
placeholder="Catagory"
value={activity.category}
component={SelectInput}
options={category}
/>
<Form.Group widths="equal">
<Field
component={DateInput}
name="date"
date={true}
placeholder="Date"
value={activity.date!}
/>
<Field
component={DateInput}
name="time"
time={true}
placeholder="Time"
value={activity.date!}
/>
</Form.Group>
<Field
name="city"
placeholder="City"
value={activity.city}
component={TextInput}
/>
<Field
name="venue"
component={TextInput}
placeholder="Venue"
value={activity.venue}
/>
<Button
loading={submiting}
floated="right"
positive
type="submit"
content="Submit"
/>
<Button
onClick={() => history.push('/activities')}
floated="right"
type="button"
content="Cancel"
/>
</Form>
)}
/>
</Segment>
</Grid.Column>
</Grid>
);
};
export default observer(ActivityForm);
这是错误
解决方案
也是 TypeScript 的新手,也参加了 Neil Cummings 的课程 =)。我删除了这一行,它似乎工作正常:
<DateTimePicker enter code here
placeholder={placeholder}
value={input.value || null}
onChange={input.onChange}
onBlur={input.onBlur}
onKeyDown={(e) => e.preventDefault()}
date={date}
time={time}
//{...rest}
/>
似乎问题与安装的 react-widget 版本有关
希望这对你有用
推荐阅读
- java - 在 RecyclerView.ViewHolder 中获取上下文
- window - 如何使用 PowerShell 更新 COM+ 应用程序并创建其 msi 文件?
- typescript - 将类分配给 DOM 对象的打字稿错误
- angular - 如何以角度创建“ngOnActive”生命周期挂钩?
- java - smbj:我怎样才能只获取子目录?
- r - 如何在多个拆分数据帧中强制列到行名,然后在所有数据帧(长度不等)上应用一个函数?
- angular - 如何使用反应式开关图防止多个网络请求
- c# - ASP.NET Web Api 底层提供者未能打开 mdf 已经存在
- python - python Telnet 通过 SSH 隧道
- android - firebase android中的getDownloadUrl不起作用