Skip to main content

Installation

Ziggy includes a useRoute() 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 passing Ziggy to every useRoute() call, make it available globally:
Now you can use useRoute() without passing the config:

Basic Usage

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:
Now you get full autocomplete for route names and parameters:

Strict Type Checking

Enable strict route name checking:

Hook Implementation

The useRoute() hook is implemented as follows:
The hook returns a version of 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 means useRoute() can’t find Ziggy’s config. Either:
  1. Pass the config directly: useRoute(Ziggy)
  2. Make it global: globalThis.Ziggy = Ziggy
  3. Use the @routes Blade directive

Routes not updating after changes

If you’re using ziggy: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