Trying to understand the use of useCallback in this situation with React Navigation

Viewed 268

I'm trying to learn why it's necessary to use useCallback in this situation:

function MyComponent() {
  const navigation = useContext(NavigationContext);

  const redirect = useCallback(() => {
    navigation.navigate("Home");
  });

  useEffect(() => {
    redirect();
  }, [redirect]);
}

I can't use navigation directly inside useEffect() and also I can't refer the function without useCallback(). I don't know why I can use other objects like Firebase Context (from a Firestore instance) without any problem inside useEffect, but I can't use navigation directly.

1 Answers
Related