gpt4 book ai didi

react-select - 如何在 React-Select 中使用 setValue?

转载 作者:行者123 更新时间:2023-12-05 02:02:10 25 4
gpt4 key购买 nike

文档中没有太多关于如何在 react select 中使用 setValue 函数。

这是我的问题。在我的多选中,我有一些元素,如果被选中,就会使其他元素无效。或选择2个特定元素时,必须选择第三个。

所以我做一个如果。我有一个引用,所以它看起来像这样:

if(selectInputRef.current){
if(selectInputRef.current.select.hasValue('optionA')){
selectInputRef.current.select.clearValue();
//selectInputRef.current.select.setValue('optionB','deselect-option');
}
if(selectInputRef.current.select.hasValue('optionA') && selectInputRef.current.select.hasValue('optionC')){
//selectInputRef.current.select.setValue('optionD','select-option');
}
}

所以在这项工作中所有的元素都没有注释。我可以检查该值是否被选中,如果是,我可以完全清除该选择。

但现在我需要特别更改元素。这是行不通的。该文档在这方面并不是很有用。

enter image description here

那么我应该如何编写它才能使这条线正常工作?

最佳答案

好的,这么小的更正。

hasValue('optionA') 行只会告诉您该值是否存在。如果选中则不会。

您需要执行 getValue() 来获取所有选中的选项,然后在其中进行搜索。

此外,setValue 设置选择的完整值。不需要清除。它需要完整的数据阵列。所以 setValue([{value: 'optionA', label:'Option A'}]); 将选中该选项并取消选中其他所有内容。

关于react-select - 如何在 React-Select 中使用 setValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66035144/

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