Uncaught TypeError: event.target is undefined

Tags: , ,



Using React and React-Dropdown package, I keep getting this Uncaught TypeError: event.target is undefined error whenever I select a year.

const [sYear, setSelectYear] = useState()

function newYearSetter(event) {
  setSelectYear(event.target.value)
  console.log(event.target.value)
}

return (
  <Dropdown
    options={years}
    value={sYear}
    onChange={newYearSetter}
    placeholder="Select a year"
  />
)

Answer

Looks like this package doesn’t pass in the event but only the changed value of the new selected option:

https://github.com/fraserxu/react-dropdown/blob/master/index.js#L96-L100

So you will have to change your code to something like this:

const [sYear, setSelectYear] = useState();

function newYearSetter(newValue) {
  console.log(newValue);
}

return (
  <Dropdown
    options={years}
    value={sYear}
    onChange={newYearSetter}
    placeholder="Select a year"
  />
)


Source: stackoverflow