Skip to content

React Hooks

Stratal Inertia ships two React hooks that give your components access to server-side routing and internationalization data. Both hooks are available from @stratal/inertia/react.

The useRoute hook provides type-safe URL generation and route matching inside your React components. It works like Laravel’s Ziggy — named routes defined on the server are available on the client without hardcoding paths.

Enable route sharing in your module setup:

InertiaModule.forRoot({
// ...other options
routes: true,
})
const { route, current } = useRoute()
Return valueTypeDescription
route(name, params?)(name: string, params?: Record<string, string>) => stringGenerate a URL from a named route with optional parameters
current(name?)(name?: string) => booleanCheck if the current page matches a route name. Returns true for the active route when called without arguments.
import { useRoute } from '@stratal/inertia/react'
export default function Navigation() {
const { route, current } = useRoute()
return (
<nav>
<a
href={route('posts.index')}
className={current('posts.index') ? 'active' : ''}
>
Posts
</a>
<a href={route('posts.show', { id: '123' })}>
View Post
</a>
</nav>
)
}

Route names and their parameters auto-complete in your editor when you generate types with the Quarry CLI:

Terminal window
npx quarry route:types

This scans your controllers and produces a type registry so that route('posts.show', { id: '123' }) is fully type-checked — invalid route names or missing parameters surface as compile-time errors.

The useI18n hook exposes translation functions and the active locale to your React components.

Enable i18n in your module setup:

InertiaModule.forRoot({
// ...other options
i18n: {
only: ['common', 'posts'],
},
})
const { t, locale } = useI18n()
Return valueTypeDescription
t(key, params?)(key: string, params?: Record<string, string>) => stringTranslate a message key with optional interpolation parameters
localestringThe current locale string (e.g. 'en', 'fr')
import { useI18n } from '@stratal/inertia/react'
export default function Greeting() {
const { t, locale } = useI18n()
return (
<div>
<p>{t('common.welcome', { name: 'John' })}</p>
<span>Current locale: {locale}</span>
</div>
)
}

Translation keys use dot notation where the first segment is the namespace and the rest is the path within that namespace’s messages. For example, common.welcome resolves to the welcome key in the common namespace.