My function keeps pushing duplicates and i don't know why.
Is it possible to update my code so that state contains only dates that are clicked once and removed when clicked twice?
So kind of being able to toggle dates in state.
(Because if misclicked, the user should be able te remove the selected date)
const [dates, setDates] = useState([])
const handleDayClick = (day) => {
let newArray = [...dates];
let indexItem = newArray.indexOf(day);
indexItem === -1 ? newArray.push(day) : newArray.splice(day, 1);
setDates(newArray)
};
useEffect(() => {
console.log(dates);
}, [dates]);
return (
<DayPicker selectedDays={dates} onDayClick={handleDayClick} />
)
The day parameter in handleDayClick returns values as strings like:
Fri Mar 05 2021 12:00:00 GMT+0100 (Central European Standard Time)
I know there are related questions on Stackoverflow about this but i've been trying all of them the past hour and i can't find a fix.
If someone could look into it, that would be very helpful. Thanks in advance
newArray.splice(indexItem, 1);.indexOf()doesn't use a callback, just the value. The.findIndex()method does.