I have a dropdown created with MUI Select, MenuItem, and FormControl.
The Select
's onChange
updates a state variable using useState
's callback function:
const handleDropdownChange = (e) => setDropdownVariable(e.target.value)
The state variable is a useMemo
dependency (one of several dependencies, simplified below):
const result = useMemo(() => expensiveCalculation(), [dropdownStateVariable])
Everything works as intended.
Problem: The dropdown does not close until expensiveCalculation
has finished running. The delay is only 1-2 seconds at the most, but ideally I'd like to close the dropdown before/while the process is running.
expensiveCalculation
is not fetching data, but processing it. The data-fetching is done before this point, within useEffect
.
I've chosen useMemo
for this because I anticipate the user may frequently switch between different values of dropdownStateVariable
and would like to benefit from the performance optimization of using memoized results.
Based on some preliminary research and some of the answers I've seen, it seems like useMemo
would be the correct choice for me. In that case, is there any way around the slight delay in closing the Select
dropdown? On the other hand, using useEffect
, I would be able to use a state loading variable to give immediate feedback that the request is being processed while asynchronously preparing the data - but I'd lose the benefit of memoization.
Any suggestions appreciated.