![](/img/trans.png)
[英]how can I set the default value to the textfield type datetime-local in material-ui for the current date and time?
[英]How Do I Set The Current Date In A Material-UI TextField
如果我在TextField
上设置type="date"
我可以用字符串初始化它。
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>
);
}
但是,当我尝试使用日期对象使用当前日期时
const values = {
someDate: new Date()
};
我收到一个错误
Warning: Failed prop type: Invalid prop `defaultValue` supplied to `TextField`.
如何将日期传递给 TextField?
正如您在文档中所读到的那样defaultValue
必须是string
或number
。 不支持Date
类型。
但是由于您使用的是type="date"
,因此将日期作为字符串传递应该没问题。
例如
const values = {
someDate: new Date().toISOString().substring(0, 10);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.