-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/shakacode/react_on_rails …
…into improve-json-safe-and-pretty # Conflicts: # app/helpers/react_on_rails_helper.rb
- Loading branch information
Showing
34 changed files
with
1,257 additions
and
848 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Using React Helmet to build `<head>` content | ||
|
||
## Installation and general usage | ||
See https://github.com/nfl/react-helmet for details. Run `yarn add react-helmet` in your `client` directory to add this package to your application. | ||
|
||
## Example | ||
Here is what you need to do in order to configure your Rails application to work with **ReactHelmet**. | ||
|
||
Create generator function for server rendering like this: | ||
|
||
```javascript | ||
export default (props, _railsContext) => { | ||
const componentHtml = renderToString(<App {...props} />); | ||
const helmet = Helmet.renderStatic(); | ||
|
||
const renderedHtml = { | ||
componentHtml, | ||
title: helmet.title.toString(), | ||
}; | ||
return { renderedHtml }; | ||
}; | ||
``` | ||
You can add more **helmet** properties to result, e.g. **meta**, **base** and so on. See https://github.com/nfl/react-helmet#server-usage. | ||
|
||
Use regular component or generator function for client-side: | ||
|
||
```javascript | ||
export default (props, _railsContext) => ( | ||
<App {...props} /> | ||
); | ||
``` | ||
|
||
Put **ReactHelmet** component somewhere in your `<App>`: | ||
```javascript | ||
import { Helmet } from 'react-helmet'; | ||
|
||
const App = (props) => ( | ||
<div> | ||
<Helmet> | ||
<title>Custom page title</title> | ||
</Helmet> | ||
... | ||
</div> | ||
); | ||
|
||
export default App; | ||
``` | ||
Register your generators for client and server sides: | ||
|
||
```javascript | ||
import ReactHelmetApp from '../ReactHelmetClientApp'; | ||
|
||
ReactOnRails.register({ | ||
ReactHelmetApp | ||
}); | ||
``` | ||
```javascript | ||
import ReactHelmetApp from '../ReactHelmetServerApp'; | ||
|
||
ReactOnRails.register({ | ||
ReactHelmetApp | ||
}); | ||
``` | ||
Now when `react_component` helper will be called with **"ReactHelmetApp"** as a first argument it will return a hash instead of HTML string: | ||
```ruby | ||
<% react_helmet_app = react_component("ReactHelmetApp", prerender: true, props: { hello: "world" }, trace: true) %> | ||
<% content_for :title do %> | ||
<%= react_helmet_app['title'] %> | ||
<% end %> | ||
|
||
<%= react_helmet_app["componentHtml"] %> | ||
``` | ||
So now we're able to insert received title tag to our application layout: | ||
```ruby | ||
<%= yield(:title) if content_for?(:title) %> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...eact_on_rails/templates/base/base/client/app/bundles/HelloWorld/components/HelloWorld.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...act_on_rails/templates/redux/base/client/app/bundles/HelloWorld/components/HelloWorld.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.