简体   繁体   English

当我尝试更新创建的 object 时,React-select 填充 null 值选项

[英]React-select fills an null value option when I try to update a created object

I'm using react-select for choosing members for a project based on a team list.我正在使用react-select根据团队列表为项目选择成员。

The format of the options is as specified in the docs (array of objects with label and value)选项的格式在文档中指定(具有 label 和值的对象数组)

const options = [
     {label: "Sam Altman", value: "61b5b1a4f401d574f5cefab7"},
     {label: "Sam B", value: "87tgb1a4f401d574f5cefab7"},
     {label: "John Altman", value: "9o2nb1a4f401d574f5sd347"},
]

The problem arises when I'm trying to update the values in a different view.当我尝试在不同的视图中更新值时,就会出现问题。 The selected values contains null option (where I auto populate the form with the created values )选定的值包含 null 选项(我用创建的值自动填充表单

在此处输入图像描述

My current select component is as follows: -我目前的 select 组件如下:-

<Select
    isMulti
    options={options}
    value={item}
    onChange={(newMembers) =>
    setValues({ ...values, assignedTo: newMembers || [] })
    }
  />

The problem only arises while updating the created members仅在更新创建的成员时出现问题

use values instead of value .使用values而不是value below is sample code.下面是示例代码。

import { useState } from 'react';

import Select from 'react-select';

const options = [
  { label: "Sam Altman", value: "61b5b1a4f401d574f5cefab7" },
  { label: "Sam B", value: "87tgb1a4f401d574f5cefab7" },
  { label: "John Altman", value: "9o2nb1a4f401d574f5sd347" }
];

function App() {

  const [values, setVaues] = useState([]);

  return (
    <div>
      <Select
        isMulti
        values={values}
        onChange={(newMembers) => setVaues({ ...values, assignedTo: newMembers || [] })} options={options}
      />
    </div>
  );
}

export default App;

在此处输入图像描述

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

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