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

Using Rails View Helpers In React? #913

Closed
hasahmed opened this issue Jul 25, 2018 · 3 comments
Closed

Using Rails View Helpers In React? #913

hasahmed opened this issue Jul 25, 2018 · 3 comments

Comments

@hasahmed
Copy link

System configuration

Sprockets or Webpacker version:
3.12.0
React-Rails version:
?
Rect_UJS version:
?
Rails version:
5.1.6
Ruby version:
2.5.1


I want to be able to use rails view helpers from React. Is this possible?
I want to do this because as it is I need to pass a lot of things into the react component using the react_component view helpers hash argument, then from there I have to continue passing the objects down using props. This seems clunky and prone to error, when there is only one component in the hierarchy that worries about the object in the first place. Is there a react-rails solution to this? This would only be possible if server-render were set to true I assume.

The specific issue that I am having looks is this:
I have a View component that is a template for the entire page. It has the Nav-Bar and the Footer and then the children of the View component that make up the rest of the page vary. The Nave-Bar component is different based on weather or not the user is logged in. As I have it currently I am passing the information down the hierarchy through props, but it seems quite clunky. I have a rails helper method. Is there any way to access it without passing it down via props?

@BookOfGreg
Copy link
Member

Sadly not.

React and Rails can only talk through the props interface. There was an Issue open for someone to work on passing Rails html through as children but that also seems infeasable to me. Basically everything is passed to the ExecJS Runtime and it runs in isolation and returns the server-side rendered react as a string.

This must run in isolation because if you think of the client-side story, there is no Rails server for the client to access!

@ttanimichi
Copy link
Member

Is there any way to access it without passing it down via props?

I think you can use Redux to avoid passing objects down by using props. You can create a store and inject props to components by using mapStateToProps.

@hasahmed
Copy link
Author

@BookOfGreg Yes that makes sense. Would be nice though!
@ttanimichi Cool thanks for the heads up.

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

No branches or pull requests

3 participants