Installation
Ziggy includes auseRoute() hook that makes it easy to use the route() helper in your React components.
Basic Setup with @routes
If you’re using the@routes Blade directive, Ziggy’s config is available globally. Simply import and use the hook:
Setup Without @routes
If you’re not using@routes, import Ziggy’s configuration and pass it to useRoute():
Making Config Global
To avoid passingZiggy to every useRoute() call, make it available globally:
useRoute() without passing the config:
Basic Usage
Simple Links
Links with Parameters
Dynamic Lists
React Router Integration
Use Ziggy with React Router for client-side navigation:Pass
false as the third argument to route() to generate relative URLs without the domain.Form Handling
POST Requests with axios
Update Requests
Data Fetching
Using fetch API
Custom Hook for Data Fetching
TypeScript Support
Basic TypeScript Usage
With Generated Types
First, generate Ziggy’s TypeScript definitions:Strict Type Checking
Enable strict route name checking:Hook Implementation
TheuseRoute() hook is implemented as follows:
route() that automatically uses the config you passed to useRoute(), or falls back to the global Ziggy variable.
Server-Side Rendering (SSR)
For SSR frameworks like Next.js, ensure Ziggy’s config is available on both server and client:Troubleshooting
”Missing configuration” error
This error meansuseRoute() can’t find Ziggy’s config. Either:
- Pass the config directly:
useRoute(Ziggy) - Make it global:
globalThis.Ziggy = Ziggy - Use the
@routesBlade directive
Routes not updating after changes
If you’re usingziggy:generate, re-run the command after changing routes:
TypeScript errors with route names
Generate type definitions:Next Steps
TypeScript
Set up full type safety and autocompletion
Vue
Use Ziggy with Vue instead
Route Helper
Learn all route() function capabilities
JavaScript Frameworks
General framework integration guide