I am building a React component that allows a user to click a button to move the date forward by 10 days. My code seems to update the state 'selectedDate' but the component does not re-render. I have tried swapping the state to a number (e.g 500) and having it increment by + 1 each time and that does make the component re-render. For some reason, it won't re-render when a state is a Date object.
import React from 'react';
import { useState, useEffect } from 'react';
const BrowserHeader = () => {
const today = new Date();
const [selectedDate, setSelectedDate] = useState(today);
const onButtonClick = () => {
const currentDate = selectedDate;
currentDate.setDate(currentDate.getDate()+10);
setSelectedDate(currentDate);
console.log('selectedDate is now:', selectedDate);
};
return (
<div className='browser-header'>
<button>
<i className="fas fa-fast-backward"></i>
</button>
<form>
<label>Date:</label>
<input
value={selectedDate}
onChange={e => setSelectedDate(e.target.value)}
/>
<button><i className="fas fa-sync-alt"></i></button>
</form>
<button onClick={onButtonClick}>
<i className="fas fa-fast-forward" ></i>
</button>
</div>
);
};
export default BrowserHeader;