Trigger toast notifications from your Vue or React components using the useToast composable/hook
You can trigger toast notifications directly from your frontend components using the useToast() composable (Vue) or hook (React). This is useful for client-side interactions like copy-to-clipboard, form submissions, or any action that doesn’t require a server round-trip.
The useToast hook returns the same methods for triggering toasts:
Copy
import { useToast } from '@laravel-inertia-toast/react'function MyComponent() { const { success, error, info, warning } = useToast() function handleCopy() { navigator.clipboard.writeText('some text') success('Copied to clipboard!') } function handleDelete() { // Perform deletion error('Item has been removed.', { title: 'Deleted' }) } return ( <div> <button onClick={handleCopy}>Copy</button> <button onClick={handleDelete}>Delete</button> </div> )}
The useToast hook must be used within a <ToastProvider> component. If you try to use it outside the provider, you’ll get an error. We recommend wrapping your whole app with the <ToastProvider>.
<script setup>import { useToast } from '@laravel-inertia-toast/vue'const { info } = useToast()function showTip() { info('Press Ctrl+K to open command palette')}</script>
Copy
import { useToast } from '@laravel-inertia-toast/react'function TipButton() { const { info } = useToast() return ( <button onClick={() => info('Press Ctrl+K to open command palette')}> Show Tip </button> )}
Override the default auto-dismiss duration (in milliseconds):
Vue
React
Copy
<script setup>import { useToast } from '@laravel-inertia-toast/vue'const { info } = useToast()// Show for 10 secondsinfo('This is a longer message.', { duration: 10000 })</script>
Copy
import { useToast } from '@laravel-inertia-toast/react'function Component() { const { info } = useToast() // Show for 10 seconds info('This is a longer message.', { duration: 10000 })}
interface ToastConfig { duration: number // Default duration in milliseconds position: string // Toast position on screen maxVisible: number // Maximum number of visible toasts propKey: string // Inertia flash key}