Unable to clear cache and make another API call based on another dropdown in Class Component.
See original GitHub issueAre you submitting a bug report or a feature request or a question?
Question
Hi,
My use case here is depending upon contentTypeDropdown, I should display the options by search in react-select-async-paginate by making an API call. So whenever I change contentTypeDropdown value the cache should be cleared and make an API call to get records of the selected contentTypeDropdown value.
Thanks.
What is the current behavior?
Unable to clear the cache and unable to make API call after a change in contentTypeDropdown.
What is the expected behavior?
The cache should be cleared and make an API call to display options.
Code
` import React, { Component } from ‘react’; import { Nav, Form } from ‘react-bootstrap’; import { AsyncPaginate } from ‘react-select-async-paginate’;
class SearchBoxDropDown extends Component {
constructor(props) {
super(props);
this.state = {
content_type: '3',
value: {},
};
}
handleContentTypeChange = (e) => {
this.setState({ content_type: e.target.value });
}
handleChange = (value) => {
this.setState({ value });
}
loadOptions = async (search, prevOptions) => {
const { content_type } = this.state;
const { contentTypeDropdown, globalSearch } = this.props;
let filteredOptions = [];
let hasMore = false;
// Example of contentTypeDropdown
// contentTypeDropdown = [{ id: 1, model: 'apple' }, { id: 2, model: 'banana' }, { id: 3, model: 'cherry' },
// { id: 4, model: 'pineapple' }, { id: 5, model: 'guava' }, { id: 6, model: 'mango' },
// { id: 7, model: 'kiwi' }, { id: 8, model: 'coconut' }];
if (search) {
const type = contentTypeDropdown.filter(item => item.id === content_type)[0].model;
// globalSearch is a function that makes an API call and fetches data.
// response.next has the value null if there are no records load.
await globalSearch(type, search, prevOptions.length).then((response) => {
filteredOptions = response.results.map(item => ({ value: item.id, label: item.name }));
hasMore = response.next !== null;
});
}
return {
options: filteredOptions,
hasMore,
};
}
render() {
const { contentTypeDropdown } = this.props;
const { content_type, value } = this.state;
return (
<>
<Nav.Item as="li" style={{ display: 'flex'}}>
<Form.Control
as="select"
name="content_type"
className="ff-Arial rounded-0"
style={{ backgroundColor: '#50b8e4', color: 'white' }}
value={content_type}
onChange={this.handleContentTypeChange}
>
{contentTypeDropdown.map(val => <option style={{ backgroundColor: 'white', color: 'black' }} key={val.id} value={val.id}>{val.model}</option>)}
</Form.Control>
</Nav.Item>
<Nav.Item as="li" style={{ width: '49%', marginTop: '15px' }}>
<AsyncPaginate
value={value}
loadOptions={this.loadOptions}
onChange={this.handleChange}
defaultOptions
cacheUniqs={[content_type]}
/>
</Nav.Item>
</>
);
}
}
export default SearchBoxDropDown;
`
What’s your environment?
“react”: “^16.8.6”, “react-select”: “^3.1.0”, “react-select-async-paginate”: “^0.4.0-alpha.1”,
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
How to clear ng-select selection - Stack Overflow
I was expecting a clear() method or something similar, but the documented API doesn't have anything along those lines. This is my dropdown...
Read more >How To Call Web APIs with the useEffect Hook in React
In this tutorial, you'll use the useEffect and useState React Hooks to fetch and display information in a sample application, ...
Read more >Application Data Caching - Quarkus
Calling this method WILL NOT invalidate values cached by the load method because the key elements order is different. Generating a cache key...
Read more >Suspense for Data Fetching (Experimental) - React
We call this approach “fetch-on-render” because it doesn't start fetching until after the component has rendered on the screen. This leads to a...
Read more >Options for optimizing caching in React - LogRocket Blog
In the case of different inputs, the cache gets invalidated. ... Suppose that we have to add two huge numbers in a component...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I added another state variable “clearCache” to cacheUniqs prop like this —> cacheUniqs={[content_type, clearCache]} and changed the value of “clearCache” whenever I open the dropdown using “onMenuOpen” prop.
A “cacheUniqs” prop receives an array of states to get a specific cache. But depending on the state, the component will not always notice this change, that’s why Kaushikkurapati created an auxiliary state.
But in general all you need to do is create a state for a specific type of cache. For example: I have two “dropdown selects”, the first chooses the type (seller or customer) and the second lists the options.
The first “select” changes the type of the second, something like this:
And in the “select async” I set to clear the cache whenever the “type” changes, like this: