0

I created a calendar in a react-native app and I would like to display a blue circle around the date that I have clicked. But I don't success to display it.

This is my code:

 const [currentDate, setCurrentDate] = useState(moment()
     .utcOffset('+01:00')
     .format('YYYY-MM-DD'));
 const [dateLimited, setDateLimited] = useState(moment()
    .utcOffset('+01:00')
    .format('YYYY-MM-DD'));

return(
     <Calendar markedDates={{currentDate: {selected: true} }} style={{ width: wp('80%'), height: hp('30%') }} minDate={'2020-01-01'} maxDate={dateLimited} onDayPress={(day)=> setCurrentDate( moment(day.dateString).format('YYYY-MM-DD'))}></Calendar>
);

What is strange is that when I write the date directly like this it works:

<Calendar markedDates={{'2020-04-28': {selected: true} }} style={{ width: wp('80%'), height: hp('30%') }} minDate={'2020-01-01'} maxDate={dateLimited} 
                onDayPress={(day)=> setCurrentDate( moment(day.dateString).format('YYYY-MM-DD'))}></Calendar>

And if I display in console.log currentDate, I got the right date :

2020-04-28

Could you please help me ?

Thanks in advance

Malaury Boudon
  • 656
  • 1
  • 8
  • 18

1 Answers1

1

See the first comment of this SO question:JavaScript set object key by variable

To evaluate currentDate instead of just using it as a key, you have to put square brackets around: markedDates={[currentDate]: {selected: true}}

Sinder
  • 273
  • 1
  • 11