react_on_rails 11.1.7 → 12.0.0.pre.beta.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +320 -0
- data/.eslintignore +2 -1
- data/.eslintrc +30 -2
- data/.github/FUNDING.yml +1 -0
- data/.gitignore +3 -1
- data/.prettierignore +10 -0
- data/.prettierrc +23 -0
- data/.release-it.json +3 -0
- data/.rubocop.yml +37 -11
- data/.travis.yml +10 -20
- data/CHANGELOG.md +86 -3
- data/CONTRIBUTING.md +61 -80
- data/Gemfile +3 -5
- data/{COMM-LICENSE → REACT-ON-RAILS-PRO-LICENSE} +6 -9
- data/README.md +117 -68
- data/Rakefile +0 -7
- data/SUMMARY.md +12 -12
- data/book.json +5 -5
- data/docs/additional-reading/asset-pipeline.md +8 -16
- data/docs/additional-reading/images.md +1 -1
- data/docs/additional-reading/rails_view_rendering_from_inline_javascript.md +2 -1
- data/docs/additional-reading/react-helmet.md +30 -10
- data/docs/additional-reading/react-router.md +52 -75
- data/docs/additional-reading/server-rendering-tips.md +12 -7
- data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +10 -0
- data/docs/api/javascript-api.md +3 -3
- data/docs/api/redux-store-api.md +4 -2
- data/docs/api/view-helpers-api.md +17 -14
- data/docs/basics/configuration.md +64 -61
- data/docs/basics/deployment.md +1 -2
- data/docs/basics/generator-details.md +1 -1
- data/docs/basics/i18n.md +44 -22
- data/docs/basics/installation-into-an-existing-rails-app.md +2 -2
- data/docs/basics/minitest-configuration.md +31 -0
- data/docs/basics/react-server-rendering.md +3 -1
- data/docs/basics/recommended-project-structure.md +24 -1
- data/docs/basics/{generator-functions-and-railscontext.md → render-functions-and-railscontext.md} +59 -21
- data/docs/basics/rspec-configuration.md +2 -2
- data/docs/basics/upgrading-react-on-rails.md +61 -3
- data/docs/basics/webpack-configuration.md +26 -1
- data/docs/contributor-info/errors-with-hooks.md +45 -0
- data/docs/contributor-info/pull-requests.md +44 -0
- data/docs/misc/doctrine.md +1 -1
- data/docs/{misc-pending → outdated}/code-splitting.md +13 -9
- data/docs/{additional-reading → outdated}/heroku-deployment.md +0 -6
- data/docs/{basics → outdated}/how-react-on-rails-works.md +2 -2
- data/docs/{misc-pending → outdated}/manual-installation-overview.md +5 -5
- data/docs/{additional-reading → outdated}/rails-assets-relative-paths.md +3 -3
- data/docs/{misc-pending → outdated}/rails-assets.md +4 -7
- data/docs/{misc → outdated}/rails3.md +0 -0
- data/docs/testimonials/hvmn.md +25 -0
- data/docs/testimonials/resortpass.md +13 -0
- data/docs/testimonials/testimonials.md +28 -0
- data/docs/tutorial.md +157 -25
- data/jest.config.js +4 -0
- data/lib/generators/react_on_rails/dev_tests_generator.rb +2 -1
- data/lib/generators/react_on_rails/generator_helper.rb +4 -6
- data/lib/generators/react_on_rails/install_generator.rb +2 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +9 -8
- data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +4 -8
- data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/store/helloWorldStore.js +1 -3
- data/lib/react_on_rails.rb +4 -1
- data/lib/react_on_rails/configuration.rb +15 -23
- data/lib/react_on_rails/error.rb +2 -0
- data/lib/react_on_rails/git_utils.rb +2 -0
- data/lib/react_on_rails/helper.rb +110 -159
- data/lib/react_on_rails/json_output.rb +1 -1
- data/lib/react_on_rails/json_parse_error.rb +2 -0
- data/lib/react_on_rails/locales/base.rb +142 -0
- data/lib/react_on_rails/locales/to_js.rb +37 -0
- data/lib/react_on_rails/locales/to_json.rb +27 -0
- data/lib/react_on_rails/prerender_error.rb +11 -15
- data/lib/react_on_rails/react_component/render_options.rb +4 -0
- data/lib/react_on_rails/server_rendering_js_code.rb +42 -0
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +85 -60
- data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +7 -8
- data/lib/react_on_rails/utils.rb +19 -20
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/version_checker.rb +5 -1
- data/lib/react_on_rails/webpacker_utils.rb +21 -2
- data/lib/tasks/assets.rake +5 -45
- data/lib/tasks/locale.rake +8 -2
- data/package-scripts.yml +49 -0
- data/package.json +41 -31
- data/rakelib/dummy_apps.rake +1 -9
- data/rakelib/example_type.rb +3 -1
- data/rakelib/examples.rake +3 -0
- data/rakelib/lint.rake +2 -7
- data/rakelib/node_package.rake +2 -2
- data/rakelib/release.rake +3 -2
- data/rakelib/run_rspec.rake +5 -18
- data/react_on_rails.gemspec +3 -5
- data/tsconfig.json +14 -0
- data/webpackConfigLoader.js +5 -4
- data/yarn.lock +7042 -2327
- metadata +40 -57
- data/Gemfile.rails32 +0 -74
- data/docs/additional-reading/babel.md +0 -5
- data/docs/additional-reading/hot-reloading-rails-development.md +0 -57
- data/docs/api/ruby-api-hot-reload-view-helpers.md +0 -44
- data/docs/testimonials.md +0 -11
- data/lib/react_on_rails/assets_precompile.rb +0 -150
- data/lib/react_on_rails/locales_to_js.rb +0 -136
data/book.json
CHANGED
@@ -7,12 +7,12 @@
|
|
7
7
|
"url": "https://github.com/shakacode/react_on_rails/"
|
8
8
|
},
|
9
9
|
"sharing": {
|
10
|
-
"facebook":
|
11
|
-
"twitter":
|
12
|
-
"google":
|
13
|
-
"weibo":
|
10
|
+
"facebook": true,
|
11
|
+
"twitter": true,
|
12
|
+
"google": true,
|
13
|
+
"weibo": true,
|
14
14
|
"instapaper": true,
|
15
|
-
"vk":
|
15
|
+
"vk": true
|
16
16
|
}
|
17
17
|
}
|
18
18
|
}
|
@@ -1,20 +1,12 @@
|
|
1
|
-
# Asset Pipeline
|
1
|
+
# Asset Pipeline with React on Rails
|
2
2
|
|
3
|
-
|
3
|
+
In general, you should not be mixing the asset pipeline with rails/webpacker and React on Rails.
|
4
4
|
|
5
|
-
|
5
|
+
If you're using React, then all of your CSS and images should be under either `/client` or
|
6
|
+
`/app/javascript` or wherever you want your client side application.
|
6
7
|
|
8
|
+
If you are incrementally migrating a large application, your main concern will be how to minimize
|
9
|
+
duplication of styles and images between your old application and the new one.
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
This option still works for your `/config/initializers/react_on_rails.rb` if you are still using the
|
11
|
-
asset pipeline.
|
12
|
-
```
|
13
|
-
################################################################################
|
14
|
-
# MISCELLANEOUS OPTIONS
|
15
|
-
################################################################################
|
16
|
-
# If you want to use webpack for CSS and images, and still use the asset pipeline,
|
17
|
-
# see https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/rails-assets.md
|
18
|
-
# And you will use a setting like this.
|
19
|
-
config.symlink_non_digested_assets_regex = /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/
|
20
|
-
```
|
11
|
+
Please email [justin@shakacode.com](mailto:justin@shakacode.com) if you would be interested in help
|
12
|
+
to migrate a larger application.
|
@@ -39,7 +39,7 @@ const assetLoaderRules = [
|
|
39
39
|
|
40
40
|
|
41
41
|
|
42
|
-
A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.
|
42
|
+
A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.jsx](../../spec/dummy/client/app/components/ImageExample/ImageExample.jsx)
|
43
43
|
|
44
44
|
You are free to use images either in image tags or as background images in SCSS files. You can
|
45
45
|
use a "global" location of /client/app/assets/images or a relative path to your JS or SCSS file, as
|
@@ -12,9 +12,10 @@ You can easily render React components in your JavaScript with `render` method t
|
|
12
12
|
* @param name Name of your registered component
|
13
13
|
* @param props Props to pass to your component
|
14
14
|
* @param domNodeId
|
15
|
+
* @param hydrate [optional] Pass truthy to update server rendered html. Default is falsy
|
15
16
|
* @returns {virtualDomElement} Reference to your component's backing instance
|
16
17
|
*/
|
17
|
-
ReactOnRails.render(componentName, props,
|
18
|
+
ReactOnRails.render(componentName, props, domNodeId)
|
18
19
|
```
|
19
20
|
|
20
21
|
## Why do we need this?
|
@@ -1,12 +1,13 @@
|
|
1
1
|
# Using React Helmet to build `<head>` content
|
2
2
|
|
3
3
|
## Installation and general usage
|
4
|
-
See https://github.com/nfl/react-helmet for details
|
4
|
+
See [nfl/react-helmet](https://github.com/nfl/react-helmet) for details on how to use this package.
|
5
|
+
Run `yarn add react-helmet` to add this package to your application.
|
5
6
|
|
6
7
|
## Example
|
7
8
|
Here is what you need to do in order to configure your Rails application to work with **ReactHelmet**.
|
8
9
|
|
9
|
-
Create
|
10
|
+
Create a render-function for server rendering like this:
|
10
11
|
|
11
12
|
```javascript
|
12
13
|
export default (props, _railsContext) => {
|
@@ -20,17 +21,35 @@ export default (props, _railsContext) => {
|
|
20
21
|
return { renderedHtml };
|
21
22
|
};
|
22
23
|
```
|
23
|
-
You can add more **helmet** properties to result, e.g. **meta**, **base** and so on. See https://github.com/nfl/react-helmet#server-usage.
|
24
|
+
You can add more **helmet** properties to the result, e.g. **meta**, **base** and so on. See https://github.com/nfl/react-helmet#server-usage.
|
24
25
|
|
25
|
-
Use regular component or
|
26
|
+
Use a regular React functional or class component or a render-function for your client-side bundle:
|
26
27
|
|
27
28
|
```javascript
|
28
|
-
|
29
|
+
// React functional component
|
30
|
+
export default (props) => (
|
29
31
|
<App {...props} />
|
30
32
|
);
|
31
33
|
```
|
32
34
|
|
33
|
-
|
35
|
+
Or a render-function. Note you can't return just the JSX (React element), but you need to return
|
36
|
+
either a React functional or class component.
|
37
|
+
```javascript
|
38
|
+
// React functional component
|
39
|
+
export default (props, railsContext) => (
|
40
|
+
() => <App {{railsContext, ...props}} />
|
41
|
+
);
|
42
|
+
```
|
43
|
+
|
44
|
+
Note, this doesn't work, because this function just returns a React element rather than a React component
|
45
|
+
```javascript
|
46
|
+
// React functional component
|
47
|
+
export default (props, railsContext) => (
|
48
|
+
<App {{railsContext, ...props}} />
|
49
|
+
);
|
50
|
+
```
|
51
|
+
|
52
|
+
Put the **ReactHelmet** component somewhere in your `<App>`:
|
34
53
|
```javascript
|
35
54
|
import { Helmet } from 'react-helmet';
|
36
55
|
|
@@ -55,15 +74,18 @@ ReactOnRails.register({
|
|
55
74
|
});
|
56
75
|
```
|
57
76
|
```javascript
|
77
|
+
// Note the import from the server file.
|
58
78
|
import ReactHelmetApp from '../ReactHelmetServerApp';
|
59
79
|
|
60
80
|
ReactOnRails.register({
|
61
81
|
ReactHelmetApp
|
62
82
|
});
|
63
83
|
```
|
64
|
-
Now when the `react_component_hash` helper is called with **"ReactHelmetApp"** as a first argument it
|
84
|
+
Now when the `react_component_hash` helper is called with **"ReactHelmetApp"** as a first argument it
|
85
|
+
will return a hash instead of HTML string. Note, there is no need to specify "prerender" as it would not
|
86
|
+
make sense to use react_component_hash without server rendering:
|
65
87
|
```ruby
|
66
|
-
<% react_helmet_app = react_component_hash("ReactHelmetApp",
|
88
|
+
<% react_helmet_app = react_component_hash("ReactHelmetApp", props: { hello: "world" }, trace: true) %>
|
67
89
|
|
68
90
|
<% content_for :title do %>
|
69
91
|
<%= react_helmet_app['title'] %>
|
@@ -76,5 +98,3 @@ So now we're able to insert received title tag to our application layout:
|
|
76
98
|
```ruby
|
77
99
|
<%= yield(:title) if content_for?(:title) %>
|
78
100
|
```
|
79
|
-
|
80
|
-
Note: Use of `react_component` for this functionality is deprecated. Please use `react_component_hash` instead.
|
@@ -1,39 +1,28 @@
|
|
1
|
+
_This article needs updating for the latest version of React Router_
|
2
|
+
|
1
3
|
# Using React Router
|
2
4
|
|
3
|
-
React on Rails supports the use of React Router. Client-side code doesn't need any special configuration for the React on Rails gem. Implement React Router how you normally would. Note, you might want to avoid using Turbolinks as both Turbolinks and React-Router will be trying to handle the back and forward buttons. If you get this figured out, please do share with the community! Otherwise, you might have to tweak the basic settings for Turbolinks, and this may or may not be worth the effort.
|
4
5
|
|
5
|
-
|
6
|
+
React on Rails supports the use of React Router. Client-side code doesn't need any special configuration for the React on Rails gem. Implement React Router how you normally would. Note, you might want to avoid using Turbolinks as both Turbolinks and React-Router will be trying to handle the back and forward buttons. If you get this figured out, please do share with the community! Otherwise, you might have to tweak the basic settings for Turbolinks, and this may or may not be worth the effort.
|
6
7
|
|
7
8
|
If you are working with the HelloWorldApp created by the react_on_rails generator, then the code below corresponds to the module in `client/app/bundles/HelloWorld/startup/HelloWorldApp.jsx`.
|
8
9
|
|
9
10
|
```js
|
11
|
+
|
12
|
+
import { BrowserRouter, Switch } from 'react-router-dom'
|
13
|
+
import routes from './routes.jsx'
|
14
|
+
|
10
15
|
const RouterApp = (props, railsContext) => {
|
11
|
-
let error;
|
12
|
-
let redirectLocation;
|
13
|
-
let routeProps;
|
14
|
-
const { location } = railsContext;
|
15
|
-
|
16
16
|
// create your hydrated store
|
17
17
|
const store = createStore(props);
|
18
|
-
|
19
|
-
// See https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md
|
20
|
-
match({ routes, location }, (_error, _redirectLocation, _routeProps) => {
|
21
|
-
error = _error;
|
22
|
-
redirectLocation = _redirectLocation;
|
23
|
-
routeProps = _routeProps;
|
24
|
-
});
|
25
|
-
|
26
|
-
// This tell react_on_rails to skip server rendering any HTML. Note, client rendering
|
27
|
-
// will handle the redirect. What's key is that we don't try to render.
|
28
|
-
// Critical to return the Object properties to match this { error, redirectLocation }
|
29
|
-
if (error || redirectLocation) {
|
30
|
-
return { error, redirectLocation };
|
31
|
-
}
|
32
|
-
|
33
|
-
// Important that you don't do this if you are redirecting or have an error.
|
18
|
+
|
34
19
|
return (
|
35
20
|
<Provider store={store}>
|
36
|
-
<
|
21
|
+
<BrowserRouter>
|
22
|
+
<Switch>
|
23
|
+
{routes}
|
24
|
+
</Switch>
|
25
|
+
</BrowserRouter>
|
37
26
|
</Provider>
|
38
27
|
);
|
39
28
|
};
|
@@ -50,64 +39,52 @@ For a fleshed out integration of react_on_rails with react-router, check out [Re
|
|
50
39
|
|
51
40
|
# Server Rendering Using React Router V4
|
52
41
|
|
53
|
-
Your
|
42
|
+
Your render function may not return an object with the property `renderedHtml`. Thus, you call
|
54
43
|
renderToString() and return an object with this property.
|
55
44
|
|
56
45
|
This example **only applies to server rendering** and should be only used in the server side bundle.
|
57
46
|
|
58
|
-
From the [original example in the ReactRouter docs](https://react-router.
|
47
|
+
From the [original example in the ReactRouter docs](https://github.com/ReactTraining/react-router/blob/v4.3.1/packages/react-router-dom/docs/guides/server-rendering.md)
|
59
48
|
|
60
49
|
```javascript
|
61
50
|
import React from 'react'
|
62
51
|
import { renderToString } from 'react-dom/server'
|
63
|
-
import {
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
markup = renderToString(
|
102
|
-
<ServerRouter
|
103
|
-
location={location}
|
104
|
-
context={context}
|
105
|
-
>
|
106
|
-
<App/>
|
107
|
-
</ServerRouter>
|
108
|
-
)
|
109
|
-
}
|
110
|
-
return { renderedHtml: markup };
|
111
|
-
}
|
52
|
+
import { StaticRouter } from 'react-router'
|
53
|
+
import { Provider } from 'react-redux'
|
54
|
+
import ReactOnRails from 'react-on-rails'
|
55
|
+
|
56
|
+
// App.jsx from src/client/App.jsx
|
57
|
+
import App from '../App'
|
58
|
+
|
59
|
+
const ReactServerRenderer = (props, railsContext) => {
|
60
|
+
const context = {}
|
61
|
+
|
62
|
+
// commentStore from src/server/store/commentStore
|
63
|
+
const store = ReactOnRails.getStore('../store/commentStore')
|
64
|
+
|
65
|
+
// Route Store generated from react-on-rails
|
66
|
+
|
67
|
+
const { location } = railsContext
|
68
|
+
|
69
|
+
const html = ReactDOMServer.renderToString(
|
70
|
+
<Provider store={store}>
|
71
|
+
<StaticRouter
|
72
|
+
location={location}
|
73
|
+
context={context}
|
74
|
+
props={props}
|
75
|
+
>
|
76
|
+
<App />
|
77
|
+
</StaticRouter>
|
78
|
+
</ Provider>
|
79
|
+
)
|
80
|
+
|
81
|
+
if (context.url) {
|
82
|
+
// Somewhere a `<Redirect>` was rendered
|
83
|
+
redirect(301, context.url)
|
84
|
+
} else {
|
85
|
+
// we're good, send the response
|
86
|
+
return { renderedHtml: html };
|
87
|
+
}
|
88
|
+
}
|
112
89
|
}
|
113
90
|
```
|
@@ -1,14 +1,19 @@
|
|
1
1
|
# Server Rendering Tips
|
2
2
|
|
3
|
-
|
3
|
+
For the best performance with Server Rendering, consider using [React on Rails Pro]
|
4
|
+
|
5
|
+
Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_on_rails/issues/428).
|
6
|
+
|
7
|
+
|
4
8
|
|
5
9
|
## General Tips
|
6
|
-
- Your code can't reference `document`. Server side JS execution does not have access to `document`,
|
7
|
-
other libs won't work in this environment. You can debug this by putting in
|
8
|
-
statements in your code.
|
9
|
-
- You can conditionally avoid running code that references document by
|
10
|
-
|
11
|
-
|
10
|
+
- Your code can't reference `document`. Server side JS execution does not have access to `document`,
|
11
|
+
so jQuery and some other libs won't work in this environment. You can debug this by putting in
|
12
|
+
`console.log` statements in your code.
|
13
|
+
- You can conditionally avoid running code that references document by either checking if `window`
|
14
|
+
is defined or using the "railsContext"
|
15
|
+
your top level react component. Since the passed in props Hash from the view helper applies to
|
16
|
+
client and server side code, the best way to do this is to use a render function.
|
12
17
|
- If you're serious about server rendering, it's worth the effort to have different entry points for client and server rendering. It's worth the extra complexity. The point is that you have separate files for top level client or server side, and you pass some extra option indicating that rendering is happening server side.
|
13
18
|
- You can enable Node.js server rendering via [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
|
14
19
|
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Upgrading rails/webpacker v3.5 to v4
|
2
|
+
|
3
|
+
The following steps can be followed to update a Webpacker v3.5 app to v4.
|
4
|
+
|
5
|
+
1. Update the gem `webpacker` and the package `@rails/webpacker`
|
6
|
+
1. Merge changes from the new default [.babelrc](../lib/install/config/.babelrc) to your `/.babelrc`. If you are using React, you need to add `"@babel/preset-react"`, to the list of `presets`.
|
7
|
+
1. Copy the file [.browserslistrc](../lib/install/config/.browserslistrc) to `/`.
|
8
|
+
1. Merge any differences between [config/webpacker.yml](../lib/install/config/webpacker.yml) and your `/config/webpacker.yml`.
|
9
|
+
|
10
|
+
Here is an [example commit of these changes](https://github.com/shakacode/react_on_rails-tutorial-v11/pull/1/files).
|
data/docs/api/javascript-api.md
CHANGED
@@ -25,10 +25,10 @@ The best source of docs is the main [ReactOnRails.js](https://github.com/shakaco
|
|
25
25
|
/**
|
26
26
|
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
|
27
27
|
* find you components for rendering. Components get called with props, or you may use a
|
28
|
-
* "
|
28
|
+
* "render function" to return a React component or an object with the following shape:
|
29
29
|
* { renderedHtml, redirectLocation, error }.
|
30
|
-
* For server rendering, if you wish to return multiple HTML strings from a
|
31
|
-
* you may return an Object from your
|
30
|
+
* For server rendering, if you wish to return multiple HTML strings from a render function,
|
31
|
+
* you may return an Object from your render function with a single top level property of
|
32
32
|
* renderedHtml. Inside this Object, place a key called componentHtml, along with any other
|
33
33
|
* needed keys. This is useful when you using side effects libraries like react helmet.
|
34
34
|
* Your Ruby code with get this Object as a Hash containing keys componentHtml and any other
|
data/docs/api/redux-store-api.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Redux Store
|
2
2
|
|
3
|
-
|
3
|
+
_This redux API is no longer recommended as it prevents dynamic code splitting for performance. Instead, you should use the standard react_component view helper passing in a "render function."_
|
4
4
|
|
5
|
-
|
5
|
+
You don't need to use the `redux_store` api to use redux. This api was setup to support multiple calls to `react_component` on one page that all talk to the same redux store.
|
6
|
+
|
7
|
+
If you are only rendering one react component on a page, as is typical to do a "Single Page App" in React, then you should _probably_ pass the props to your React component in a "render function."
|
6
8
|
|
7
9
|
Consider using the `redux_store` helper for the two following use cases:
|
8
10
|
|
@@ -10,21 +10,25 @@ Once the bundled files have been generated in your `app/assets/webpack` folder a
|
|
10
10
|
```ruby
|
11
11
|
react_component(component_name,
|
12
12
|
props: {},
|
13
|
-
prerender: nil
|
14
|
-
trace: nil,
|
15
|
-
replay_console: nil,
|
16
|
-
raise_on_prerender_error: nil,
|
17
|
-
id: nil,
|
13
|
+
prerender: nil)
|
18
14
|
html_options: {})
|
19
15
|
```
|
20
16
|
|
21
|
-
|
17
|
+
Uncommonly used options:
|
18
|
+
```
|
19
|
+
trace: nil,
|
20
|
+
replay_console: nil,
|
21
|
+
raise_on_prerender_error: nil,
|
22
|
+
id: nil,
|
23
|
+
```
|
24
|
+
|
25
|
+
- **component_name:** Can be a React component, created using an ES6 class or a render function that returns a React component (or, only on the server side, an object with shape { redirectLocation, error, renderedHtml }), or a "renderer function" that manually renders a React component to the dom (client side only).
|
22
26
|
All options except `props, id, html_options` will inherit from your `react_on_rails.rb` initializer, as described [here](../basics/configuration.md).
|
23
27
|
- **general options:**
|
24
28
|
- **props:** Ruby Hash which contains the properties to pass to the react object, or a JSON string. If you pass a string, we'll escape it for you.
|
25
29
|
- **prerender:** enable server-side rendering of a component. Set to false when debugging!
|
26
30
|
- **id:** Id for the div, will be used to attach the React component. This will get assigned automatically if you do not provide an id. Must be unique.
|
27
|
-
- **html_options:** Any other HTML options get placed on the added div for the component. For example, you can set a class (or inline style) on the outer div so that it behaves like a span, with the styling of `display:inline-block`.
|
31
|
+
- **html_options:** Any other HTML options get placed on the added div for the component. For example, you can set a class (or inline style) on the outer div so that it behaves like a span, with the styling of `display:inline-block`. You may also use an option of `tag: "span"` to replace the use of the default DIV tag to be a SPAN tag.
|
28
32
|
- **trace:** set to true to print additional debugging information in the browser. Defaults to true for development, off otherwise. Only on the **client side** will you will see the `railsContext` and your props.
|
29
33
|
- **random_dom_id:** True to automatically generate random dom ids when using multiple instances of the same React component on one Rails view.
|
30
34
|
- **options if prerender (server rendering) is true:**
|
@@ -41,7 +45,7 @@ adding meta-tags to a page. It is exactly like react_component except for the fo
|
|
41
45
|
|
42
46
|
1. `prerender: true` is automatically added to options, as this method doesn't make sense for
|
43
47
|
client only rendering.
|
44
|
-
2. Your JavaScript for server rendering must return an Object
|
48
|
+
2. Your JavaScript render function for server rendering must return an Object rather than a React Component.
|
45
49
|
3. Your view code must expect an object and not a string.
|
46
50
|
|
47
51
|
Here is an example of ERB view code:
|
@@ -69,7 +73,6 @@ export default (props, _railsContext) => {
|
|
69
73
|
};
|
70
74
|
return { renderedHtml };
|
71
75
|
};
|
72
|
-
|
73
76
|
```
|
74
77
|
|
75
78
|
------------
|
@@ -89,17 +92,17 @@ end %>
|
|
89
92
|
|
90
93
|
### rails_context
|
91
94
|
|
92
|
-
You can call `rails_context(server_side: true
|
95
|
+
You can call `rails_context` or `rails_context(server_side: true|false)` from your controller or view to see what values are are in the Rails Context. Pass true or false depending on whether you want to see the server side or the client side rails_context. Typically, for computing cache keys, you should leave server_side as the default true. When calling this from a controller method, use `helpers.rails_context`.
|
93
96
|
|
94
97
|
------------
|
95
98
|
|
96
99
|
### Renderer Functions (function that will call ReactDOM.render or ReactDOM.hydrate)
|
97
100
|
|
98
|
-
A "renderer function" is a
|
101
|
+
A "renderer function" is a render function that accepts three arguments (rather than 2): `(props, railsContext, domNodeId) => { ... }`. Instead of returning a React component, a renderer is responsible for installing a callback that will call `ReactDOM.render` (in React 16+, `ReactDOM.hydrate`) to render a React component into the DOM. The "renderer function" is called at the same time the document ready event would instantate the React components into the DOM.
|
99
102
|
|
100
|
-
Why would you want to call `ReactDOM.hydrate` yourself? One possible use case is [code splitting](
|
103
|
+
Why would you want to call `ReactDOM.hydrate` yourself? One possible use case is [code splitting](docs/outdated/code-splitting.md). In a nutshell, you don't want to load the React component on the DOM node yet. So you want to install some handler that will call `ReactDOM.hydrate` at a later time. In the case of code splitting with server rendering, the server rendered code has any async code loaded and used to server render. Thus, the client code must also fully load any asynch code before server rendering. Otherwise, the client code would first render partially, not matching the server rendering, and then a second later, the full code would render, resulting in an unpleasant flashing on the screen.
|
101
104
|
|
102
|
-
Renderer functions are not meant to be used on the server since there's no DOM on the server. Instead, use a
|
105
|
+
Renderer functions are not meant to be used on the server since there's no DOM on the server. Instead, use a render function. Attempting to server render with a renderer function will throw an error.
|
103
106
|
|
104
107
|
------------
|
105
108
|
|
@@ -109,7 +112,7 @@ Renderer functions are not meant to be used on the server since there's no DOM o
|
|
109
112
|
|
110
113
|
1. [React on Rails docs for react-router](../additional-reading/react-router.md)
|
111
114
|
2. Examples in [spec/dummy/app/views/react_router](../../spec/dummy/app/views/react_router) and follow to the JavaScript code in the [spec/dummy/client/app/startup/ServerRouterApp.jsx](../../spec/dummy/client/app/startup/ServerRouterApp.jsx).
|
112
|
-
3. [Code Splitting docs](
|
115
|
+
3. [Code Splitting docs](docs/outdated/code-splitting.md) for information about how to set up code splitting for server rendered routes.
|
113
116
|
|
114
117
|
------------
|
115
118
|
|