Skip to content
Advertisement

Uncaught TypeError: event.target is undefined

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"
  />
)

Advertisement

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"
  />
)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement