Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention React usage in Readme #327

Closed
lodzen opened this issue Sep 24, 2020 · 6 comments · Fixed by #473
Closed

Mention React usage in Readme #327

lodzen opened this issue Sep 24, 2020 · 6 comments · Fixed by #473
Assignees
Labels

Comments

@lodzen
Copy link

lodzen commented Sep 24, 2020

Description

I struggled a lot with the usage in a React Application.
The problem was that i don't want to put the @routes blade directive in the header of my application because everyone can see all routes directly if the source code is inspected.

Due to this i wanted to go with option two to generate the Ziggy file and use it as custom Ziggy in the route() function.
In the Readme i could only find the Vue solution to use a mixin for this purpose but nothing related to React.

Suggestion

I wrote a very small helper that did the trick for me.
Maybe this one can also be mentioned in the Readme to save some time i someone has the same issue.
I called the file ziggyroutes and imported it in my application in the view where i need it.
Additionally the helper methods are mentioned in the function like current(), check() and url().

import route from '../../assets/ziggyroute';

console.log(route('user.login')); 
import route from 'ziggy';
import { Ziggy } from './ziggy';

export default function (name, params, absolute) {
    var self = {name: name, params: params, absolute: absolute};

    var routeshelper = {};
    
    routeshelper.current = (name) => {
        return route(undefined, undefined, undefined, Ziggy).current(name);
    }
    routeshelper.check = (name) => {
        return route(undefined, undefined, undefined, Ziggy).check(name);
    }
    routeshelper.url = () => {
        return route(self.name, undefined, undefined, Ziggy).url();
    }
    
    return route(name, params, absolute, Ziggy);
  }

If there are any improvements for the small helper please feel free to change it.

@bakerkretzmar bakerkretzmar self-assigned this Sep 24, 2020
@bakerkretzmar
Copy link
Collaborator

bakerkretzmar commented Sep 24, 2020

I don't understand exactly what the helper accomplishes here. Can you just do

import route from 'ziggy';
import { Ziggy } from './ziggy';

right where you actually need to use the route() function?

@lodzen
Copy link
Author

lodzen commented Sep 24, 2020

Yes i could do this also but then i need to mention every time the custom Ziggy as argument to the route function call.

@bakerkretzmar
Copy link
Collaborator

@lodzen great point 😂 I'll think about how we can make this easier for React users, and in the meantime I'll update the Readme to link here. Thanks!

@bakerkretzmar
Copy link
Collaborator

bakerkretzmar commented Sep 25, 2020

I took a quick stab at a Hook that does something quite similar to your helper: useRoute (Ziggy Hook)

I could see this being something that might be worth including in Ziggy out of the box.

@lodzen
Copy link
Author

lodzen commented Sep 26, 2020

This looks great and i would appreciate it if this would be part of Ziggy.

@bakerkretzmar bakerkretzmar added this to the v1.1 milestone Oct 24, 2020
@deantheiceman
Copy link

How do we use this hook without having to import the Ziggy file everytime?

@bakerkretzmar bakerkretzmar removed this from the v1.1 milestone Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants