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

Exposing rails path helpers in React components #423

Closed
bowd opened this issue May 16, 2016 · 2 comments
Closed

Exposing rails path helpers in React components #423

bowd opened this issue May 16, 2016 · 2 comments
Labels

Comments

@bowd
Copy link
Contributor

bowd commented May 16, 2016

Context

What I'm working on won't be a SPA (at least not in the initial version). I want Rails to handle routing and rendering React components (with turbolinks). What I'm envisioning is a shared store approach and one route will have a couple of block components server side rendered which all get the store and that's where all app state is injected. I wanted this so to not mix app state through component props + store state, and keep data in a single place (this might not be the best way).

The problem

I'm not sure how to expose URLs from the rails app to the client. Of course the idea would be to have "prop serialisers" or "store state serialisers" computed in Rails that use the path helpers to expose all the component needs, but I imagine that there are subtle situations where this can get out of hand.
I mean, when you think about an isolated component props makes sense, but if the component that actually needs the URL is somewhere down the chain, you will want to but that in the store somehow so that you avoid the "big ball of props" from your top-level component. And if you want to put these URLs in the store you need to decide where they go. Dynamic URLs are resource related, that means that if you want to have a "resource" entry somewhere in the state tree you most probably have a resource serialiser that will include the URL, but then you're thinking, hmmm what if a page only cares about the URL for a small component, should I serialise the whole resource in the state tree to keep it consistent, or start hacking things around.

This was a bit of a brain dump sort of description, so I hope it isn't too hard to follow but what I'm after is basically is to see how other people are handling this.

@justin808
Copy link
Member

I suggest using this features to make the relevant paths available in all the JS code:

https://github.com/shakacode/react_on_rails/blob/master/README.md#customization-of-the-rails_context

@dkniffin
Copy link

dkniffin commented Jun 8, 2018

For anyone else that comes looking for this, I wanted to mention an alternative I found.

There's a gem called js-routes that is purpose-built for exposing Rails routes in JS. Unfortunately, it doesn't (yet) support webpacker out of the box, but there's an easy workaround here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants