[英]react useState not working with new Date() as initial value
I want to use current Date as a state in my component using useState hooks.我想使用 useState 挂钩在我的组件中将当前日期用作 state。 So I made this...
所以我做了这个...
import React, { useState } from "react";
import Navbar from "./components/Navbar/Navbar";
import Date from "./components/Date/Date";
import "./App.css";
const App = () => {
const [date, setDate] = useState(new Date());
return (
<div className="App">
<Navbar />
<Date selectedDate={date} />
</div>
);
};
export default App;
But it throws this error when I am setting my initial date as a new Date object.但是当我将初始日期设置为新日期 object 时,它会引发此错误。
Your component is named Date
, like the Date
object you trying to use:您的组件名为
Date
,就像您尝试使用的Date
object 一样:
// Rename
import DateComponent from "./components/Date/Date";
// Will call the constructor of Date object
new Date();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.