Uncaught TypeError: event.target is undefined

Uncaught TypeError: event.target is undefined

使用 React 和 React-Dropdown 包,我每年 select 时都会收到此 Uncaught TypeError: event.target is undefined 错误。

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

看起来 this package 没有传递事件,只有新选择选项的更改值:

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

因此您必须将代码更改为如下所示:

const [sYear, setSelectYear] = useState();

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

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