useRouter
The useRouter hook provides access to navigation methods for programmatic routing. It returns an object with push and replace methods to navigate between routes.
import { useRouter } from "@farbenmeer/router";
function LoginForm() { const router = useRouter();
const handleLogin = async (credentials) => { try { await login(credentials); router.push("/dashboard"); } catch (error) { console.error("Login failed:", error); } };
return ( <form onSubmit={handleLogin}> {/* form fields */} </form> );}Return Value
Section titled “Return Value”The hook returns an object with the following methods:
push(url: string)
Section titled “push(url: string)”- Description: Navigate to a new route by adding a new entry to the browser’s history stack
- Parameters:
url(string): The destination URL (absolute path, relative path, or full URL with query parameters)
- Returns:
void
const router = useRouter();
// Navigate to absolute pathrouter.push("/users");
// Navigate with parametersrouter.push("/users/123");
// Navigate with query parametersrouter.push("/search?q=react");
// Navigate with hashrouter.push("/docs#installation");
// Navigate with everythingrouter.push("/products?category=electronics&sort=price#top");replace(url: string)
Section titled “replace(url: string)”- Description: Navigate to a new route by replacing the current entry in the browser’s history stack
- Parameters:
url(string): The destination URL (absolute path, relative path, or full URL with query parameters)
- Returns:
void
const router = useRouter();
// Replace current history entryrouter.replace("/login");
// Useful for redirects where you don't want users to go backrouter.replace("/dashboard");Navigation Methods Comparison
Section titled “Navigation Methods Comparison”| Method | History Stack | Use Case |
|---|---|---|
push | Adds new entry | Normal navigation, allows back button |
replace | Replaces current entry | Redirects, login flows, error corrections |