简体   繁体   中英

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. 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.

在此处输入图像描述

Your component is named Date , like the Date object you trying to use:

// Rename
import DateComponent from "./components/Date/Date";

// Will call the constructor of Date object
new Date();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM