简体   繁体   English

在多模式下以编程方式清除/重置 React-Select label?

[英]Programmatically clear/reset React-Select label in multi mode?

I am new to react and trying to maintain/ update some existing code in an application我是新来的反应并试图维护/更新应用程序中的一些现有代码

In a form, we have a react-select custom/extended component for which the values are set using 'onChange' of react-select.在一个表单中,我们有一个 react-select 自定义/扩展组件,其值是使用 react-select 的“onChange”设置的。 In this case, we have an issue with resetting the selected label even when the state is reset (during component re-rendering, not initial rendering)在这种情况下,即使 state 被重置(在组件重新渲染期间,而不是初始渲染期间),我们也会在重置选定的 label 时遇到问题

I have tried the below code for non-multi select component and it did work, ie forced selected labels to re-render in the UI when state value is reset我已经为非多 select 组件尝试了以下代码,它确实有效,即当 state 值重置时,强制选定的标签在 UI 中重新呈现

key={`my_unique_select_key__${selected}`}

however when i have a 'multi' select component, the above code is not suitable since there can be more than 1 'selected' value.但是,当我有一个“多”select 组件时,上面的代码不适合,因为可以有超过 1 个“选定”值。 Tried various options for 'key' property ie generated list of keys selected by using map function on selected values list but didn't work for me尝试了 'key' 属性的各种选项,即使用 map function 在选定值列表上生成的密钥列表,但对我不起作用

Also i cannot pass null for 'value' property, since we are using 'onChange' to set values.此外,我无法将 null 传递给“value”属性,因为我们正在使用“onChange”来设置值。 ie trying to set the value property will conflict with existing onChange functionality for setting value(s).即尝试设置 value 属性将与用于设置 value(s) 的现有 onChange 功能冲突。

Not sure how i can resolve this issue since i am still exploring available/ other options.不确定如何解决此问题,因为我仍在探索可用/其他选项。

Note: code mentioned above is part of the earlier post How to programmatically clear/reset React-Select?注意:上面提到的代码是之前文章的一部分如何以编程方式清除/重置 React-Select?

You could also try by setting the value to an empty string.您也可以尝试将值设置为空字符串。 For example:例如:

    document.getElementById('TagOfTheLabel').value = "";

And if it results with an error, try:如果它导致错误,请尝试:

    document.getElementById('TagOfTheLabel').innerHTML = "";

Note: if it wants atleast a single charecter or string, replace "";注意:如果它想要至少一个字符或字符串,请替换""; with " ";" "; . . In words, just add a space.换句话说,只需添加一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM