简体   繁体   中英

React-select.Async doesn't load options on onClick

I need a Async react-select, which will loadOptions from backend and users will be able to select multiple options. However when inputValue is already entered and users clicks on select input options is not being loaded. They are loaded only when you type something in.

Example code sandbox

Input value stays the same

EDIT: answer

To reload default options you need to pass defaultOptions to Async select, not just true. This can be done by setting defaultOptions to state in componentDidMount and listening for 'menu-close' action in onInputChange and setting defaultOptions to state there. This will ensure that when the component is mounted the defaultOptions will be there, and when menu is closed, the options will be refetched with new inputValue.

To have react-select autoload content you must provide the defaultOptions prop. If you don't autoload then it won't make your async call until they attempt to filter.

<AsyncSelect loadOptions={myAsyncMethod} defaultOptions />

To reload default options you need to pass defaultOptions to Async select, not just true. This can be done by setting defaultOptions to state in componentDidMount and listening for 'menu-close' action in onInputChange and setting defaultOptions to state there. This will ensure that when the component is mounted the defaultOptions will be there, and when menu is closed, the options will be refetched with new inputValue.

Working example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM