How to programmatically trigger Async loadOptions #5389
Replies: 26 comments 4 replies
-
AFAIK, loadOptions can not be triggered manually outside of Async component :( |
Beta Was this translation helpful? Give feedback.
-
One of the workaround is using When you want to tigger loadOptions again, give it a different key so it will re-render the part with react-select component. I hope we can have a proper API to trigger this outside of Async component |
Beta Was this translation helpful? Give feedback.
-
I think @phamthaibaoduy is correct, and
Instead of getting the options from Every time the user types in Select, Select calls
|
Beta Was this translation helpful? Give feedback.
-
@kinyat the key trick was genius. After struggling for an hour to get 1 select to change options in another select (and have the updated options available on first click), your key suggestion worked first time. Thank you!! |
Beta Was this translation helpful? Give feedback.
-
@alexcroox Could you please help me out? I am facing an issue similar to urs... i am fetching the value of specialitySelected from the props of my component, and modifying it in componentWillReceiveProps the state of specialitySelected is getting modified but the Select still shows old data on first click, and modified data on second click |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue here with Select V2 I have n async select and I would like to trigger a new fetch depending on other select component values. When a select is triggered with Open Menu for example, I want to refresh the list of available values. I will try to use ref possibilities and access directly internals like blur/focus to change the state of select component directly... :/ edit for Async Select with Select V2: I make it work using internal ref but it's quite a mess. Basically you can use a ref for each different select component you are using. Then when a value changed from one of my combobox, I call a specific function to update select refs : updateEndUserProductRef() {
let ref = this.endUserProductRef;
if(ref !== null) {
ref.loadOptions('', function (options) {
if (!ref.mounted) return;
let isLoading = !!ref.lastRequest;
ref.setState({ defaultOptions: options || [], isLoading: isLoading });
ref.select.select.setValue(newValues); // Update current selected values of combobox. Be careful with infinite loop here !
}
});
}
} |
Beta Was this translation helpful? Give feedback.
-
Was struggling around for a couple of hours, your suggestion worked like magic. Thank you @kinyat |
Beta Was this translation helpful? Give feedback.
-
I would really really appreciate an API to accomplish this! |
Beta Was this translation helpful? Give feedback.
-
@kinyat I have been struggling to get this work for a quite amount of time. Your suggestion worked. I prefer to have this feature as part of the Async component like disabling cache or something. |
Beta Was this translation helpful? Give feedback.
-
Can you provide an example? |
Beta Was this translation helpful? Give feedback.
-
This seems important enough to raise as an issue as this approach to the work around seems very much like a hack and could potentially have negative consequences in certain use cases. I'm adding the |
Beta Was this translation helpful? Give feedback.
-
Re - second this issue. an on focus handler would be great. |
Beta Was this translation helpful? Give feedback.
-
Another hack without ref from a duplicate post |
Beta Was this translation helpful? Give feedback.
-
Greetings @Jorybraun, react-select provides an |
Beta Was this translation helpful? Give feedback.
-
Hi, here's a simple, yet working example of a fetch of data import Select from 'react-select'
export default function LazyLoadAsyncSelect({ }) {
const [selectedValue, setSelectedValue] = useState(null)
const [options, setOptions] = useState([])
const [isLoading, setIsLoading] = useState(false)
function _onChange(object, { action }) {
switch (action) {
case 'input-change':
if (object) setSelectedValue(object)
return
case 'menu-close':
return
case 'clear':
return
case 'select-option':
if (object) setSelectedValue(object)
return
default:
return
}
}
function getOptionsAsync() {
// this is where you would hit your API async instead! I've added a timeout for demo purposes and some sample data.
setIsLoading(true)
new Promise(resolve =>
setTimeout(
() =>
resolve([
{
id: 3,
name: 'General',
},
]),
1000
)
)
.then(response => {
// map options to {label, value}
setOptions(response.map(tag => ({ label: tag.name, value: tag.id })))
})
.catch(error => {
console.error(error)
})
.finally(() => setIsLoading(false))
}
return (
<Select
value={selectedValue}
options={options}
onFocus={getOptionsAsync}
onChange={_onChange}
isLoading={isLoading}
/>
)
} |
Beta Was this translation helpful? Give feedback.
-
This is the best solution I've found and it works perfectly 🥇 |
Beta Was this translation helpful? Give feedback.
-
Both awesome solutions @kinyat and @tannerhallman. Thanks! |
Beta Was this translation helpful? Give feedback.
-
@kinyat Bro that |
Beta Was this translation helpful? Give feedback.
-
This is solved my issue.
|
Beta Was this translation helpful? Give feedback.
-
FWIW: In a form that is set to invisible when an item is added and I use the boolean with the visibility state as
|
Beta Was this translation helpful? Give feedback.
-
I am facing a strange issue regarding the purposed solution But on every key press and on scroll the menu closes
|
Beta Was this translation helpful? Give feedback.
-
I didn't have to try many cases.
|
Beta Was this translation helpful? Give feedback.
-
How do we prevent the menu from closing when we use the onMenuScrollToBottom as a loadmore function becaue it closes when the options value is changed |
Beta Was this translation helpful? Give feedback.
-
@grt-mark What about custom handling? like |
Beta Was this translation helpful? Give feedback.
-
its working now idk what happened but the menu stays open now during lazy loading haha `const AsyncSelectLazy = ({ options, onLoadMore, onChange, onSearch, isFetching }) => { <Select filterOption={createFilter({ ignoreAccents: false })} onChange={onChange} options={options} onInputChange={onSearch} onMenuScrollToBottom={onLoadMore} isLoading={isFetching} cacheOptions defaultOptions /> ); }; AsyncSelectLazy options={data} onLoadMore={handleLoadMore} isFetching={isLoading} /> |
Beta Was this translation helpful? Give feedback.
-
In my application, I have an onChangeHandler in the first react-select that makes an AJAX call to get the data for populating a second react-select on the page. It needs to populate the second one automatically, with no typing required to trigger the loadOptions. I have not seen any examples that do it like this-- i.e. in the example here, it seems to require typing into the second select to trigger the load.
Is an automatic load like I describe even possible?
Beta Was this translation helpful? Give feedback.
All reactions