Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - event.target is undefined when trying to filter React-select options

I'm pretty new to React and JavaScript, I'm trying to grab the value of a react-select dropdown option to use in a filter. The code below throws "Uncaught TypeError: event.target is undefined".

const [filter, setFilters] = useState({})

  const handleFilters = (event)=> {
    const value = event.target.value;
    setFilters({
      [event.target.name]: value,
    });
  };

  console.log(filter);

React-Select

<Select name="category" onChange={ handleFilters } options={filterOptions} className='dropdown'/>

React-Select Options

const filterOptions = [
    { value: 'value1', label: 'label1' },
    { value: 'value2', label: 'label2' },
    { value: 'value3', label: 'label3' },
    { value: 'value4', label: 'label4' }
  ];

the consolelog I'm expecting when choosing the first dropdown option would be - { category, value1 }

like image 451
epk3 Avatar asked Jan 26 '26 09:01

epk3


1 Answers

This is happening because the onChange handler of react-select directly returns the option(Object) that is selected.

So, for example event will contain {label:"label1",value:"value1"}

You can have a look at this codesandbox for reference

like image 194
Omkar Avatar answered Jan 28 '26 22:01

Omkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!