-
-
Notifications
You must be signed in to change notification settings - Fork 627
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
Allow to return multiple markup strings from react_component helper #793
Comments
Maybe even better to allow generator function on server to return |
@udovenko, could this help:
https://github.com/shakacode/react_on_rails/blob/master/README.md#server_render_js Example<pre>
<%= server_render_js("(function() { var x = ReactOnRails.getComponent('HelloString').component.world(); console.warn('ran console.warn on the server()'); return x;})()") %>
</pre> |
@justin808 I think it will not help. |
This problem could be solved (theoretically) by introducing |
What are the side effects? @udovenko This sounds legit. Please try to craft the ideal API in terms of the JS code and the helper. Once you have that, I'll work with you on the implementation. Please send me your email and I'll invite you to our slack channel. |
By side effects I mean any additional content generated during @justin808 Ok, here is how I see the usage of our potential new feature:
Then user registers created renderer. The problem here is how to detect renderer function. On client side function signature is used for it, i.e. if function takes third argument - it is a renderer. But on the server side we do not need that third argument. So this part is to be discussed:
Now user can call
|
// startup/ServerAppRenderer.js
export default (props, railsContext) => {
let error;
let redirectLocation;
let routeProps;
const { location } = railsContext;
match({ routes, location }, (_error, _redirectLocation, _routeProps) => {
error = _error;
redirectLocation = _redirectLocation;
routeProps = _routeProps;
});
if (error || redirectLocation) {
return { error, redirectLocation };
}
const store = ReactOnRails.getStore('Store');
const appComponent = (
<Provider store={store}>
<RouterContext {...routeProps}/>
</Provider>
)
const appHtml = ReactDOMServer.renderToString(appComponent);
const helmet = Helmet.renderStatic();
return { appHtml, pageTitle: helmet.title.toString(), metaTags: helmet.meta.toString()};
}; ERB<% render_hash = react_component('App', id: 'some_id', props: {...}) %>
<% content_for :title do %>
<%= render_hash['pageTitle'] %>
<% end %>
<% content_for :meta do %>
<%= render_hash['metaTags'] %>
<% end %>
<%= render_hash['appHtml'] %> API /**
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
* find you components for rendering. Components get called with props, or you may use a
* "generator function" to return a React component or an object with the following shape:
* { renderedHtml, redirectLocation, error }. renderedHtml may be an Object, which converts
* to a Ruby Hash. This is useful if you want to use something like React Helmut to return
* additional values when rendering. See the doc page /docs/additional-reading/react-helmut.md
* @param components (key is component name, value is component)
*/
register(components) |
@justin808 PR #800 |
There are some usefull libraries based on https://github.com/gaearon/react-side-effect package. For example https://github.com/nfl/react-helmet is a great package for building
<head>
contents, like<meta>
tags and<title>
.To use it on server we need to call
const helmet = Helmet.renderStatic();
after we calledReactDOMServer.renderToString(...)
and insert markup strings formhelmet
object to HTML: https://github.com/nfl/react-helmet#server-usage.To acheive this we need some kind of
react_component
helper but it should return an array of strings or aHash
. Lets call this methodreact_component_with_side_effects
for now. Here is how it could look like:This is just an example of what we could do. An implementation needs to be discussed.
The text was updated successfully, but these errors were encountered: