[英]How Do I Set The Current Date In A Material-UI TextField
If I set type="date"
on a TextField
I can initialise it with a string.如果我在
TextField
上设置type="date"
我可以用字符串初始化它。
https://codesandbox.io/s/9y6yz462op https://codesandbox.io/s/9y6yz462op
const values = {
someDate: "2017-05-24"
};
function App() {
return (
<div className="App">
<TextField
name="someDate"
label="Some Date"
InputLabelProps={{ shrink: true, required: true }}
type="date"
defaultValue={values.someDate}
/>
</div>
);
}
However, when I try to use the current date using a date object但是,当我尝试使用日期对象使用当前日期时
const values = {
someDate: new Date()
};
I get an error我收到一个错误
Warning: Failed prop type: Invalid prop `defaultValue` supplied to `TextField`.
How do I pass a Date to the TextField?如何将日期传递给 TextField?
As you can read in the docs defaultValue
has to be either a string
or a number
.正如您在文档中所读到的那样
defaultValue
必须是string
或number
。 The Date
type is not supported.不支持
Date
类型。
But since you're using type="date"
you should be fine by handing the date as string.但是由于您使用的是
type="date"
,因此将日期作为字符串传递应该没问题。
eg例如
const values = {
someDate: new Date().toISOString().substring(0, 10);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.