React-Select: "Cannot read property 'replace' of undefined" 当我输入 Select

React-Select: "Cannot read property 'replace' of undefined" when I type in the Select

我正在实施 React-Select 异步。根据他们的文档,我的代码是正确的。然而,一旦我输入内容,onInputChange 就会抛出错误 "Cannot read property 'replace' of undefined"each。如何解决这个问题?

const options=[
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]
export default function App() {
const [query, setQuery] = useState('')
  const handleInputChange = (str) => {
    const inputValue = str.replace(/\W/g, '');
    setQuery(inputValue);
    return inputValue;
  };
  return (
    <>
     <AsyncSelect
          cacheOptions
          //loadOptions={loadOptions}
          defaultOptions={options}
          onInputChange={(e)=> handleInputChange(e.target.value)}
        />
    </>
  );
}

当触发 onInputChange 事件时,handleInputChange 函数由 react-select 传递输入值。所以你不需要手动传递值。

所以改变

onInputChange={(e)=> handleInputChange(e.target.value)}

onInputChange={handleInputChange}

您还需要将 isAsync 属性设置为 true

onInputChange={(e)=> handleInputChange(ee.target.value)}。你把 'ee.target...' 而不是 'e.target...'