I have multiple buttons in react, once I click on the button, its value is saved in an array. I want, when I click on any button that its value is saved only once, after click on that button again the value should be removed from an array.
e.g.
When I press "click 1" button the array value should be ["1"] and change the button color, after second click it should be removed.
import React, {useState} from 'react';
const UserDetails = () => {
const [selecthostingOption,setselecthostingOption] = useState([]);
console.log(selecthostingOption);
const hostingChange = (e) => {
e.preventDefault();
const value = e.target.value
setselecthostingOption(selecthostingOption => [
...selecthostingOption,value
])
}
return(
<>
<button onClick={hostingChange} name="selecthostingOption" value="1">Click 1</button>
<button onClick={hostingChange} name="selecthostingOption" value="2">Click 2</button>
</>
)
}