Home>

When you click the button, it should print the date in the console, but in the end the date is knocked out when you go to this page and when you select the date in the picker. Nothing happens when the button is pressed. What's my mistake?

const [startDate, setStartDate]= useState(new Date());
  return (
    <div>        <DatePicker selected={startDate} onChange={(date)=> setStartDate(date)}/>        <button onClick={console.log(startDate)}>Hello</button>    </div>  );
  • Answer # 1
    <button onClick={()=> console.log(startDate)}>Hello</button>
  • Answer # 2
    <button onClick={()=> console.log(startDate)}>Hello</button>