How to get name attribute of a react-select using event.target.name

Viewed 36
import Select from 'react-select';

const Home = () => {

    const animals = [
        { label: 'cat', value: 'cat' },
        { label: 'dog', value: 'dog' },
        { label: 'lion', value: 'lion' },
        { label: 'eagle', value: 'eagle' },
    ]
}

return (

<>
 
 <Select 
         name="animals" 
         options={animals} 
         strong text**isMulti 
         onChange={(e) => console.log(e.target.name)} 
  />

</>

)

**in console when i select some item it will give me this error

cannot read property of undefined (reading 'name') at onChange

enter image description here

1 Answers

Because it will return an array of object(s) {label: '', value: ''} rather than an event object.

 <Select 
    name="animals" 
    options={animals} 
    isMulti 
    onChange={(value, actionMeta) => console.log(actionMeta.name)} 
 />
Related