我正在使用 React select 创建选项下拉列表。我想访问下拉列表上的标签,但也想访问与数据关联的值。例如:
import Select from 'react-select'
let options = [
{
label: Small,
change: -3
},
{
label: Medium,
change: 0
}
]
<Select id={"options"} defaultValue={options[0]} options={options} />
let selectedChange = // How can I access the change property?
let selectedLabel = document.querySelector(`#options`).textContent // This is how I have been getting the proper label
<button onClick={console.log(selectedChange, selectedLabel)}></button>
我想通过将选项更改值添加到默认价格来更改商品的显示成本,但我只想在选择中显示标签。
最佳答案
React-Select onChange
属性采用一个方法,并接收所选项目以及正在执行的操作:
function(
One of<
Object,
Array<Object>,
null,
undefined
>,
{
action required One of<
"select-option",
"deselect-option",
"remove-value",
"pop-value",
"set-value",
"clear",
"create-option"
>
}
) => undefined
作为开发人员,您可以选择如何处理从此方法返回的对象。例如,您可以检查此 Codesandbox .
关于javascript - 从 React-Select 组件获取所选值的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60398428/