Context
I have a ContextProvider around my components.
To this ContextProvider I connected a useState hook.
In one of my components I want to set the value of the useState hook.
If the value is the same value of the current state, then there is no delay.
if the value is a new value compared to the current state, then there is a 2second delay.
As weird as my statement sounds, these stack questions have the same behavior:
What I found out by debugging
- The delay is not caused by the sum (adding 1 to the previous value of the usestate), because setting a random value (for example the current datetime) will cause a 2sec delay as well
- removing useCallback has no effect
- rerenders of my own components are not causing the 2sec delay, because no components are using the context (and thus have no reason to rerender).
- using useMemo to save the usestate variables has no effect on the delay
Project configurations
React-native:0.70.6
Code
(especially notice the last two components)
Contextprovider:
import React from 'react';
type CurrentEntryContextType = {
answersBeingSaved: number;
setAnswersBeingSaved: (count: number) => void;
};
export const CurrentEntryContext = React.createContext<CurrentEntryContextType>({
answersBeingSaved: 5,
setAnswersBeingSaved: () => {},
});
Main:
function App() {
const [answersBeingSaved, setAnswersBeingSaved] = useState<number>(5);
const currentEntryContext = useMemo(() => {
return {
answersBeingSaved: answersBeingSaved,
setAnswersBeingSaved: setAnswersBeingSaved,
};
}, [answersBeingSaved]);
return (
<>
<CurrentEntryContext.Provider value={currentEntryContext}>
<MainNavigationBar />
</CurrentEntryContext.Provider>
</>
);
}
Component that changes the context with 2s delay (calling the acknowledge is what's causing the delay):
function RandomComponent() {
const currentEntryContext = useContext(CurrentEntryContext);
const acknowledgeAnswerBeingSaved = useCallback(() => {
currentEntryContext.setAnswersBeingSaved(
currentEntryContext.answersBeingSaved + 3
);
}, [currentEntryContext]);
return <></>;
}
Component that changes the context without 2s delay (this is just to demonstrate that the issue only occurs when the useState is set to a different value each time):
function RandomComponent() {
const currentEntryContext = useContext(CurrentEntryContext);
const acknowledgeAnswerBeingSaved = useCallback(() => {
const redundantVariable = currentEntryContext.answersBeingSaved + 3;
currentEntryContext.setAnswersBeingSaved(
5
);
}, [currentEntryContext]);
return <></>;
}