简体   繁体   English

如何将 Select 标签的值从 react-select 设置为 state

[英]How can I set the values of Select tag from react-select to a state

I am trying to set values which i receive when i select multiple values from tag from "react-select".我正在尝试设置当我收到来自“react-select”标签的 select 多个值时收到的值。

Currently it throws undefined, the Outcome of the state which i require when i select values from tag is:目前它抛出未定义,当我从标签中获取 select 值时,我需要的 state 的结果是:

['user._id','user._id','user._id'.....] so on depends on how many user i select 

Here is what i did so far but its unsuccessful for me.这是我到目前为止所做的,但对我来说并不成功。

  const [ selectedMembers , setSelectedMembers ] = useState<any>([]);
  const [options, setOptions] = useState([{value: "", label: ""}]);

//In useEffect i am making API Call and received the values in **res.data.data**
//the options will be displayed in select tag , 
  if(res.data.data){
      let option: any = [];
      res.data.data.map((admin, index) => {
      option.push({value: admin._id, label: admin.displayName})       
      })
      setOptions(option);
      }

  async function onChangeSelectedMembers(e){
    console.log(e)
    setSelectedMembers([...selectedMembers , e.value])
  }
//outcome of console.log(e) when i select values from Select tag is [Note : its dynamic //if i remove any user or add more user it changes accordinly :
// 0: {value: '61dfcfb71f492f4f4f589e93', label: 'hello_1'}
// 1: {value: '61dedd23bd15322626dd7539', label: 'hello_2'}

//DISPLAY SELECT TAG WITH VALUES 
return(
   <label className="form-label">{intl.formatMessage({ id: 'SELECT.MEMBERS' })}</label>
      <Select
         className="form-select, form-select-solid, basic-multi-select"
         isMulti
         onChange={(e)=>onChangeSelectedMembers(e)}
         options={options}
       >
 )

Here's so far my trial but i am received "undefined" values in selectedMembers .到目前为止,这是我的试用版,但我在selectedMembers中收到了“未定义”值。 Can anyone help me with this?谁能帮我这个? will be greatly appreciated !将不胜感激! Thanks !谢谢 !

You need to fix your onChangeSelectedMembers function. Please try to use as follows:您需要修复您的 onChangeSelectedMembers function。请尝试使用如下:

const [ selectedMembers , setSelectedMembers ] = useState<any[]>([]);
const [options, setOptions] = useState([{value: "", label: ""}]);

//In useEffect i am making API Call and received the values in **res.data.data**
//the options will be displayed in select tag , 
if(res.data.data){
    let option: any[] = [];
    res.data.data.map((admin, index) => {
      option.push({value: admin._id, label: admin.displayName})       
    })
    setOptions(option);
}

function onChangeSelectedMembers(opts){
    console.log(opts)
    setSelectedMembers(opts.map(opt => opt));
}
//outcome of console.log(e) when i select values from Select tag is [Note : its dynamic //if i remove any user or add more user it changes accordinly :
// 0: {value: '61dfcfb71f492f4f4f589e93', label: 'hello_1'}
// 1: {value: '61dedd23bd15322626dd7539', label: 'hello_2'}

//DISPLAY SELECT TAG WITH VALUES 
return(
   <label className="form-label">{intl.formatMessage({ id: 'SELECT.MEMBERS' })}</label>
   <Select
      className="form-select, form-select-solid, basic-multi-select"
      isMulti
      value={selectedMembers}
      onChange={onChangeSelectedMembers}
      options={options}
   >
 )

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

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