首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将对象数组传递给useHook(Downshift Js)会产生错误。

将对象数组传递给useHook(Downshift Js)会产生错误。
EN

Stack Overflow用户
提问于 2020-11-08 21:51:34
回答 1查看 358关注 0票数 0

我遵循了基本用法--在下移中--示例,但将下拉列表中显示的项数组从星战(字符串数组)更改为shared.js中的对象数组(例如Id: 1,描述:‘item’)。

我还记录了一些事情,并将它们内联到下面的代码中,前面是"==>“。

当我启动应用程序时,会出现下拉列表。但是,当我单击下拉列表右侧的下拉箭头时,将显示以下错误。

未登录错误:对象作为React子对象无效(找到:键{Id,Description}的对象)。如果您打算呈现一个子集合,请使用数组代替。

对我做错了什么有什么想法吗?

代码语言:javascript
复制
import { items, menuStyles, comboboxStyles, itemToString } from "../../shared";

function DropdownCombobox() {
  const [inputItems, setInputItems] = useState(items);
  console.log(items);      // ==> Array(127)
  console.log(itemToString); // ==> i => i ? i.Description : ""
  console.log(items[1]); // ==> {Id: 38, Description: "001 - RAB"}
  const {
    isOpen,
    getToggleButtonProps,
    getLabelProps,
    getMenuProps,
    getInputProps,
    getComboboxProps,
    highlightedIndex,
    getItemProps,
  } = useCombobox({
    items: inputItems,
    itemToString,
    onInputValueChange: ({ inputValue }) => {
      setInputItems(
        items.filter((item) =>
          itemToString(item).toLowerCase().startsWith(inputValue.toLowerCase())
        )
      );
    },
  });```
EN

回答 1

Stack Overflow用户

发布于 2020-11-13 21:50:05

对象数组的项列表({Id:0,Description:'abc'} )应该是

代码语言:javascript
复制
<ul {...getMenuProps()} style={menuStyles}>
  {isOpen &&
    inputItems.map((item, index) => (
      <li
        style={highlightedIndex === index ? { backgroundColor: '#bde4ff' } : {}}
        key={item.Id}
        {...getItemProps({ item, index })}
      >
        {item.Description}
      </li>
    ))}
</ul>

,下面的处理程序应该更改

代码语言:javascript
复制
onInputValueChange: ({ inputValue }) => {
  setInputItems(
    items.filter((item) => item.Description.toLowerCase().startsWith(inputValue.toLowerCase()))
  );
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64743409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档