title: Only add item to array if unique otherwise delete - React
body: <p>My function keeps pushing duplicates and i don't know why.</p>
  <p>Is it possible to update my code so that <code>state</code> contains only dates that are clicked once and removed when clicked twice?</p>
  <p>So kind of being able to toggle dates in state.</p>
  <p>(Because if misclicked, the user should be able te remove the selected date)</p>
  <pre><code>const [dates, setDates] = useState([])
  
  const handleDayClick = (day) =&gt; {
      let newArray = [...dates];
      let indexItem = newArray.indexOf(day);
      indexItem === -1 ? newArray.push(day) : newArray.splice(day, 1);
      setDates(newArray)
    };
  
    useEffect(() =&gt; {
      console.log(dates);
    }, [dates]);
  
    return (
       &lt;DayPicker selectedDays={dates} onDayClick={handleDayClick} /&gt;
    )
  
  </code></pre>
  <p>The <code>day</code> parameter in <code>handleDayClick</code> returns values as strings like:</p>
  <pre><code>Fri Mar 05 2021 12:00:00 GMT+0100 (Central European Standard Time)
  </code></pre>
  <p>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.</p>
  <p>If someone could look into it, that would be very helpful.
  Thanks in advance</p>