1

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. To be clearer, since i'm using Typescript, I can't compile my component without importing the route function somehow.

My stack is Laravel 8, Inertia, React.

2
  • What's the error thrown? I'm working with ziggy and react with no problems. Commented Sep 22, 2020 at 20:50
  • The ziggy route() function is defined globally so you can access it anywhere in your react component. Commented Sep 22, 2020 at 21:02

2 Answers 2

2

I encountered the same problem and here is my solution to use ziggy-js in typescript.

  1. import route from 'ziggy-js'
  2. install @types/ziggy-js as you're using typescript with npm install --save-dev @types/ziggy-js
Sign up to request clarification or add additional context in comments.

1 Comment

And don't forget to do it github.com/tighten/ziggy#installation
1

In your app.blade.php make sure to pass @route as it is in the head section. ie: ` some links here @route --> pass it here

` then you can access it in your component: 1-import route from "ziggy-js"; 2-import { InertiaLink } from "@inertiajs/inertia-react";
<InertiaLinkhref={route("your_route_name", { key: value }).url()}>View</InertiaLink>

Let me know if that helps you out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.