In my custom combobox I am changing the items pagination mode onClick like below:
onClick={() => {
changeState(itemsSelectorType, elements[i]);
fetchItems(currentPagination, 0);
}}
(itemsSelectorType = currentPagination and elements[i] = 24 or 48)
changeState is changing the currentPagination value in my regex store and then in fetchItems() one of my arguments (currentPagination) is the one from store. The problem is that fetchItems reads the old value of the currentPagination, so I need to await for the changeState function.
I tried something like this but it still reads the old value, f.e. when I am choosing 24 it reads 48, when I am choosing 48 it reads 24.
onClick={async () => {
await changeState(itemsSelectorType, elements[i]);
fetchItems(currentPagination, 0);
}}
You need to get data after pagination mode changed.
There are two options.
First of all, you know what currentPagination will look like with the selected elements[i]. You can pass it directly like this.
fetchItems(pagination_value, 0)
Second, you can call fetchItems whenever currentPagination changed. Insert this code instead of calling fetchItems in other place.
React.useEffect(() => {
fetchItems(currentPagination, 0)
}, [currentPagination]);