【问题标题】:Why is my array not updating when I set state in react hooks为什么当我在反应钩子中设置状态时我的数组没有更新
【发布时间】:2021-03-17 16:20:08
【问题描述】:

我正在尝试以下拉列表的形式动态更新此数组,但它显示为空

 const setCountries = async (e) =>{
    if(e != null)
    {
      const countries = await survey.retrieveCountries(e);
      let tmpArray = [] as any;
      for (var i = 0; i < countries.length; i++) {
         tmpArray.push(countries[i].name)
      }
      setCountryDropdown(tmpArray);
    }
  }
<Multiselect
            options={countryDropdown} // Options to display in the dropdown
            displayValue="Countries To Field" // Property name to display in the dropdown options
            onSearch={(e) => setCountries(e)}
            />

【问题讨论】:

  • 你用过图书馆吗?
  • 这能回答你的问题吗? React does not re-render when state changes
  • 演员阵容至少是可疑的。 retrieveCountries 期待什么?该函数返回什么?
  • 在不知道Multiselect 是如何实现的或者您试图确认它没有改变的情况下,我认为这个问题需要更多的支持信息。

标签: reactjs


【解决方案1】:

查看the demos,如果您使用纯字符串数组作为选项,您似乎必须使用isObject={false} 作为多选的道具:

<Multiselect options={plainArray} isObject={false} />

如果你不这样做,就会让选项看起来是空的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-12
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多