I'm creating a dropdown using list items in React and am mapping through the data to get each option displayed. I want the user to be able to select multiple options and for all selected options to be saved in the state.
Currently the last option they select is being saved in an array in the state but I cannot get all of their selected options to be saved in the state because they aren't able to select multiple so they are only stored in the previous state.
How can I store multiple selected list items in an array in the state?
const [selectedSymptoms, setSelectedSymptoms] = useState(null)
const handleSymptoms = (symptom) => {
setSelectedSymptoms([symptom.Name])
// selectedSymptoms !== null && selectedSymptoms.push(symptom.Name)
console.log(selectedSymptoms)
}
<button
className={styles.selectSymptomBtn}
type="button"
onClick={toggleSymptoms}
>
Select your symptom
</button>
<ul className={`${isSymptomsOpen ? styles.show : styles.hide}`}>
{data.symptoms.map((symptom) => (
<li onClick={(() => handleSymptoms(symptom))}>
{symptom.Name}
</li>))}</ul>