0

I am implementing React-Select Async. According to their documentation, my code is correct. Yet, onInputChange throws the error "Cannot read property 'replace' of undefined"each as soon as I type something. How to fix this?

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)}
        />
    </>
  );
}

2 Answers 2

1

when onInputChange event is fired, handleInputChange function is passed the input value by react-select. So yo don't need to pass the value manually.

So change

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

to

onInputChange={handleInputChange}

Also you need to set isAsync prop to true

Edit happy-easley-w08wf

Sign up to request clarification or add additional context in comments.

2 Comments

It's a typo in my question, but there's no such error in my code. The issue persists. If I write onChange of their onInputChange, I don't have error anymore, but the filter doesn't work.
0

onInputChange={(e)=> handleInputChange(ee.target.value)} . You put 'ee.target...' instead of 'e.target...'

1 Comment

It's a typo in my question, but there's no such error in my code. The issue persists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.