question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I use Ziggy with React and Inertia ?

See original GitHub issue

How can I call in a React component the JavaScript route function generated by the Ziggy’s @route directive ?

The route function is generated at runtime so it’s impossible to import it beforehand in the react component and therefore, Laravel Mix throws an error and can’t compile the project.

My stack is Laravel 8, Inertia & React.

Thanks !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bakerkretzmarcommented, Sep 22, 2020

You can import the route() function either from Ziggy’s vendor directory, with something like

resolve: {
    alias: {
        ziggy: path.resolve('vendor/tightenco/ziggy/src/js/route.js'),
    },
}

added to your Webpack config, or by installing the ziggy-js npm package.

Ziggy provides an artisan command, php artisan ziggy:generate, that will generate the object with all of your route information and put it either at resources/assets/js/ziggy.js or at the path you pass it.

After doing those two things, you should be able to just

import route from 'ziggy'; // or `ziggy-js` if you use the npm package
import Ziggy from './ziggy'; // path to the file generated by the artisan command

and use route() normally.

Take a look at the Artisan Command section in the readme too.

I’m going to close this but let me know if that works and if you have any other questions!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Ziggy with React and Inertia - Stack Overflow
The ziggy route() function is defined globally so you can access it anywhere in your react component. – chuysbz. Sep 22, 2020 at...
Read more >
Using Ziggy with Inertia Server-Side Rendering - Aaron Francis
The first thing we need to do is get all the routes into Ziggy. In a standard Laravel app this is done with...
Read more >
09 - Using the ziggy library with InertiaJS for handling routes
Laravel 8 step by step guide | Bookmark app using React, Inertia Js and deployment to Linux · Getting Started With Laravel and...
Read more >
What is @routes in app.blade.php [inertia-react app]?
I can see @routes directive in app.blade.php in my inertia-react app. ... @routes import all Laravel routes in order to use them in...
Read more >
Routing - Inertia.js
With Inertia all routing is defined server-side. Meaning you don't need Vue Router or React Router. Simply create routes using your server-side framework...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found