Reputation: 47
I see in https://reactrouter.com/en/main/hooks/use-navigate at "It's usually better to use redirect in loaders and actions than this hook" So should I change useNavigate to redirect instead right ?
Upvotes: 3
Views: 1997
Reputation: 202618
So should I change useNavigate to redirect instead right?
No, not at all. Use the useNavigate
hook, and returned navigate
function to issue imperative navigation actions just as it's always been done/used in react-router-dom@6
since it was introduced.
[email protected]
introduced many new features relying on the new router creators that add much more functionality to the behavior of the routing context, specifically the new data APIs.
In this specific case it's informing you that you if the intent is to redirect in response to some fetched data, do it in the loader with redirect
instead of returning data to the route, mounting the routed component, and issuing the navigate
via a useEffect
hook. It saves you a few steps if you know you are going to navigate away from the route anyway.
Upvotes: 3