Reputation: 211
I am having a state object in React which has a Date value.
const [myDate, setMyDate] = useState(new Date());
How do I increment the date by one day using setMyDate() ?
Upvotes: 0
Views: 1563
Reputation: 353
You can use moment.js from npm.
setMyDate(moment(myDate).add(1, 'days').format());
Upvotes: 0
Reputation: 12010
You should return a different object in setMyDate
so that React can tell it has been modified, try:
setMyDate(date => {
date.setDate(date.getDate() + 1);
return new Date(date)
});
Upvotes: 1