gpt4 book ai didi

reactjs - 如何以编程方式清除和选择项目

转载 作者:行者123 更新时间:2023-12-03 13:23:57 24 4
gpt4 key购买 nike

我正在使用https://ant.design/components/select/

如何以编程方式从 <Select> 中删除所选项目?
注意:<Option>不是字符串值,而是节点。

最佳答案

如果您使用 React Hooks,请使用以下内容:

import React, { useState } from 'react'
import { Button, Select } from 'antd'

const { Option } = Select

// inside your component
const ComponentName = () => {
const [selected, setSelected] = useState()

// handler
const clearSelected = () => {
// this line will clear the select
// when you click on the button
setSelected(null)
}

// in the return value
return (
<>
// ...
// In the select element
<Select style={{ width: 150 }} onChange={value => setSelected(value)}
value={selected}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
<Button onClick={clearSelected}>Clear Selected</Button>
</>
)
}

关于reactjs - 如何以编程方式清除和选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47415280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com