useNavigate
The useNavigate hook imperatively navigates between routes.
Example code
component.client.jsx
import {useNavigate} from '@shopify/hydrogen';
function addToCart() {}
export default function ClientComponent() {
const navigate = useNavigate();
async function clickAddToCart() {
await addToCart();
navigate('/success', {replace: true});
}
return <Button onClick={clickAddToCart}>Add to Cart</Button>;
}
Return value
The useNavigate hook returns a function which accepts the following values:
| Name | Description |
|---|---|
| path | The path you want to navigate to. |
| options | The options for the configuration object: replace, reloadDocument, clientState, scroll. For more information on the options, refer to the Link component. |
Considerations
- Consider using the
useNavigatehook only where appropriate. Generally, you should use theLinkcomponent instead, because it provides standard browser accessibility functionality, likecmd+clickand right-click to open. - The
useNavigatehook is only available in client components.