Home > OS >  How can I set the values of Select tag from react-select to a state
How can I set the values of Select tag from react-select to a state

Time:12-29

I am trying to set values which i receive when i select multiple values from tag from "react-select".

Currently it throws undefined , the Outcome of the state which i require when i select values from tag is :

['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 . Can anyone help me with this ? will be greatly appreciated ! Thanks !

CodePudding user response:

You need to fix your onChangeSelectedMembers function. Please try to use as follows:

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}
   >
 )
  • Related