我正在使用Reaction中的列表项创建一个下拉列表,并映射数据以显示每个选项.我希望用户能够 Select 多个选项,并将所有选定的选项保存在州中.
目前,他们 Select 的最后一个选项保存在该州的数组中,但我无法将他们 Select 的所有选项保存在该州中,因为他们不能 Select 多个选项,因此它们只存储在前一个状态中.
如何将多个选定的列表项存储在州中的一个数组中?
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>