Limenius / ReactRenderer

Client and Server-side React rendering from PHP
MIT License
238 stars 37 forks source link

Provide railsContext to generator functions #4

Closed teameh closed 7 years ago

teameh commented 7 years ago

Follow up on https://github.com/Limenius/ReactRenderer/pull/3

See https://github.com/shakacode/react_on_rails/pull/345/files#diff-741e57aaaefab9559b7892657ec8c75eL270

Instead of passing the location we should pass a railsContext now. I'm not sure how this would be implemented best. The quickest way would be to pass a context to our function:

{{ react_component('RecipesApp', {'props': props, 'context': context}) }}

But it would be nicer if the user would not have to do this manually.. Any thoughts?

I would be really nice to have data available in our components. Their version is now providing this as the context:

  {
    # URL settings
    href: request.original_url,
    location: "#{uri.path}#{uri.query.present? ? "?#{uri.query}": ""}",
    scheme: uri.scheme, # http
    host: uri.host, # foo.com
    port: uri.port,
    pathname: uri.path, # /posts
    search: uri.query, # id=30&limit=5

    # Other
    serverSide: boolean # Are we being called on the server or client? NOTE, if you conditionally
     # render something different on the server than the client, then React will only show the
     # server version!
  }

I'm already passing the location as a prop to my components now because we don't had this yet.

railsContext should be passed to the store generator as well btw.

nacmartin commented 7 years ago

Thanks, @teameh, for this issue and the PRs!

I am working right now on providing a context. I will have hopefully a PR soon that provides the same context than react on rails. Indeed, passing the location as props, was not the ideal solution.

teameh commented 7 years ago

Perfect! No problem! Looking forward to it :)!

teameh commented 7 years ago

This works great! I've updated the Silex provider as well!

teameh commented 7 years ago

Will release the update to the provider once you've merged https://github.com/Limenius/ReactRenderer/pull/9. It fixes an exception.