Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How trigger AsyncSelect to reload options in ReactJS

I have two AsyncSelect components to select a category and a subcategory. When we choose a category from the first AsyncSelect, I need to load it's sub-categories to the second one.

AsyncSelect loads the items when it gets mounted. But how can I trigger it programmatically (When I select the category from the first AsyncSelect) to load the data (The subcategories to the second AsyncSelect) when it is already mounted?

 <AsyncSelect
    cacheOptions
    loadOptions={this.loadSubCategories}
    defaultOptions
    onInputChange={this.handleInputChange}
 />
like image 344
sumedhe Avatar asked May 12 '26 16:05

sumedhe


2 Answers

Add the key into your subCategories's asyncSelect.

<AsyncSelect
    key={this.state.categoryName}
    cacheOptions
    loadOptions={this.loadSubCategories}
    defaultOptions
    onInputChange={this.handleInputChange}
 />

When you select the category, please change the state.

this.setState({categoryName: "Different NAME HERE"})
like image 90
webstar Avatar answered May 15 '26 07:05

webstar


No, you can not load on the basis of other triggers. as given in document: defaultOption

There are two valid values for defaultOptions property.

1)Providing an option array to this prop will populate the initial set of options (this does the job for categories) and

2) filtering the options (typing in the control). Providing the prop by itself (or with 'true') tells the control to immediately fire the remote request, (this is only happen in the same <AsyncSelect/>)

and your requirement is not meet to load <AsyncSelect/> for subcategory

like image 22
Navin Gelot Avatar answered May 15 '26 06:05

Navin Gelot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!