reactjslaravelroutesclient-sideinertiajs

Laravel Inertia check current route In JSX file?


I need to check current route in one of my .jsx files to display a specific component, is there a way to imitate Laravel's Route::is() feature?

For now I do the following:

// helpers.js

export function IsRoute(query) {
    return query == window.location.pathname;
}

Edit: I know I can directly check the url path but if there is a more convenient way to do this I would love to know

Laravel Version: 10.44.0 PHP Version: 8.3.3 Inertia: ^0.11.1 inertia-react: ^0.8.1


Solution

  • You need this: Ziggy, Laravel routes in JavaScript

    You can use it with React, Vue or vanilla JS. Among others it has route().current() function that can check what is current route.