Duoro
Duoro

Reputation: 308

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.

enter image description here

Upvotes: 0

Views: 1802

Answers (1)

Dennis Vash
Dennis Vash

Reputation: 53894

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();

Upvotes: 4

Related Questions