[英]How to Get value of select onChange in Functional Component in React
我使用 ant 設計專業版來創建表單,但我無法使用鈎子保存 select 的值。 i 顯示錯誤錯誤
const ExistingGroup = (props) => {
const [group,setGroup] = useState([]);
const saveGroup = (event) => {
setGroup(event.target.value);
}
return (
<PageContainer header={{ title: '' }} ghost>
<Card bordered={false} style={{ width: '100%' }}>
<Form>
<Form.Item>
<Select value={group} onSelect={saveGroup}>
<Option value='1'>1</Option>
<Option value='2'>2</Option>
</Select>
{group}
</Form.Item>
</Form>
</Card>
</PageContainer>
);
};
如果您閱讀Select
的文檔,則會使用所選元素的值調用onSelect
方法。
onSelect
選擇選項時調用,參數是選項的值(或鍵)和選項實例function(string | number | LabeledValue, option: Option)
所以你要
const saveGroup = (value) => {
setGroup(value);
}
此外,由於它不是多選,因此您應該為group
使用單個值而不是數組。
const [group,setGroup] = useState(null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.