react_on_rails 11.2.2 → 12.0.0.pre.beta.3
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 +23 -1
- data/.github/FUNDING.yml +1 -0
- data/.gitignore +3 -1
- data/.prettierignore +10 -1
- data/.prettierrc +3 -0
- data/.rubocop.yml +37 -11
- data/.travis.yml +9 -20
- data/CHANGELOG.md +73 -6
- data/CONTRIBUTING.md +60 -71
- data/Gemfile +3 -4
- data/{COMM-LICENSE → REACT-ON-RAILS-PRO-LICENSE} +6 -9
- data/README.md +137 -95
- data/Rakefile +0 -7
- data/SUMMARY.md +9 -12
- data/book.json +5 -5
- data/docs/additional-reading/asset-pipeline.md +8 -16
- 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/api/javascript-api.md +3 -3
- data/docs/api/redux-store-api.md +4 -2
- data/docs/api/view-helpers-api.md +7 -8
- data/docs/basics/configuration.md +83 -69
- data/docs/basics/deployment.md +2 -4
- data/docs/basics/heroku-deployment.md +24 -0
- data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +49 -0
- data/docs/basics/i18n.md +45 -23
- data/docs/basics/installation-into-an-existing-rails-app.md +4 -9
- data/docs/basics/minitest-configuration.md +31 -0
- data/docs/basics/react-server-rendering.md +1 -1
- data/docs/basics/recommended-project-structure.md +5 -22
- data/docs/basics/{generator-functions-and-railscontext.md → render-functions-and-railscontext.md} +59 -21
- data/docs/basics/rspec-configuration.md +29 -18
- data/docs/basics/upgrading-react-on-rails.md +69 -3
- data/docs/basics/webpack-configuration.md +18 -8
- 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 +12 -8
- data/docs/{basics → outdated}/how-react-on-rails-works.md +8 -4
- 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 +2 -12
- data/docs/{misc → outdated}/rails3.md +0 -0
- data/docs/testimonials/testimonials.md +3 -3
- data/docs/tutorial.md +96 -70
- data/jest.config.js +4 -0
- data/lib/generators/react_on_rails/base_generator.rb +2 -2
- data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
- data/lib/generators/react_on_rails/generator_helper.rb +4 -6
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +3 -1
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +26 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +20 -40
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +4 -1
- 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 +3 -1
- data/lib/react_on_rails/configuration.rb +58 -28
- data/lib/react_on_rails/error.rb +2 -0
- data/lib/react_on_rails/helper.rb +48 -92
- data/lib/react_on_rails/json_parse_error.rb +2 -0
- data/lib/react_on_rails/locales/base.rb +150 -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_pool/ruby_embedded_java_script.rb +41 -46
- data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +7 -8
- data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +17 -0
- data/lib/react_on_rails/utils.rb +14 -19
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/version_checker.rb +1 -0
- data/lib/react_on_rails/webpacker_utils.rb +19 -2
- data/lib/tasks/assets.rake +29 -47
- data/lib/tasks/locale.rake +4 -2
- data/package-scripts.yml +11 -8
- data/package.json +29 -28
- 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 +0 -6
- data/rakelib/run_rspec.rake +5 -18
- data/react_on_rails.gemspec +4 -5
- data/tsconfig.json +14 -0
- data/webpackConfigLoader.js +3 -2
- data/yarn.lock +4333 -2209
- metadata +47 -57
- data/Gemfile.rails32 +0 -73
- data/docs/additional-reading/babel.md +0 -5
- data/docs/additional-reading/heroku-deployment.md +0 -92
- data/docs/additional-reading/hot-reloading-rails-development-asset-pipeline.md +0 -47
- data/docs/api/ruby-api-hot-reload-view-helpers.md +0 -44
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-server +0 -12
- data/lib/react_on_rails/assets_precompile.rb +0 -153
- data/lib/react_on_rails/locales_to_js.rb +0 -138
data/docs/basics/deployment.md
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
# Deployment
|
2
2
|
|
3
|
-
-
|
4
|
-
|
5
|
-
- `config.symlink_non_digested_assets_regex`: Default is nil, turning off the setup of non-js assets. This should be nil except when when using Sprockets rather than Webpacker.
|
6
|
-
- See the [Heroku Deployment](../additional-reading/heroku-deployment.md) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
3
|
+
- `rails/webpacker` puts the necessary precompile steps automatically in the rake precompile step.
|
4
|
+
- See the [Heroku Deployment](docs/basics/heroku-deployment.md) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Heroku Deployment
|
2
|
+
## Heroku buildpacks
|
3
|
+
|
4
|
+
React on Rails requires both a ruby environment (for Rails) and a Node environment (for Webpack), so you will need to have Heroku use multiple buildpacks.
|
5
|
+
|
6
|
+
Assuming you have downloaded and installed the Heroku command-line utility and have initialized the app, you will need to tell Heroku to use both buildpacks via the command-line:
|
7
|
+
|
8
|
+
```
|
9
|
+
heroku buildpacks:set heroku/ruby
|
10
|
+
heroku buildpacks:add --index 1 heroku/nodejs
|
11
|
+
```
|
12
|
+
|
13
|
+
For more information, see [Using Multiple Buildpacks for an App](https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app)
|
14
|
+
|
15
|
+
## assets:precompile
|
16
|
+
|
17
|
+
### rails/webpacker webpack configuration
|
18
|
+
If you're using the standard rails/webpacker configuration of webpack, then rails/webpacker
|
19
|
+
will automatically modify or create an assets:precompile task to build your assets.
|
20
|
+
|
21
|
+
### custom webpack configuration
|
22
|
+
If you're a custom webpack configuration, and you **do not have the default
|
23
|
+
`config/webpack/production.js`** file, then the `config/initializers/react_on_rails.rb`
|
24
|
+
configuration `config.build_production_command` will be used.
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# HMR and Hot Reloading with the webpack-dev-server
|
2
|
+
|
3
|
+
The webpack-dev-server provides:
|
4
|
+
|
5
|
+
1. Speedy compilation of client side assets
|
6
|
+
2. Optional HMR which means that the page will reload automatically when after
|
7
|
+
compilation completes. Note, some developers do not like this, as you'll
|
8
|
+
abruptly lose any tweaks within the Chrome development tools.
|
9
|
+
3. Optional hot-reloading. The older react-hot-loader has been deprecated in
|
10
|
+
favor of [fast-refresh](https://reactnative.dev/docs/fast-refresh).
|
11
|
+
For use with webpack, see [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin).
|
12
|
+
|
13
|
+
If you are ***not*** using server-side rendering (***not*** using `prerender: true`),
|
14
|
+
then you can follow all the regular docs for using the `bin/webpack-dev-server`
|
15
|
+
during development.
|
16
|
+
|
17
|
+
|
18
|
+
# Server Side Rendering with the Default rails/webpacker bin/webpack-dev-server
|
19
|
+
|
20
|
+
If you are using server-side rendering, then you have a couple options. The
|
21
|
+
recommended technique is to have a different webpack configuration for server
|
22
|
+
rendering.
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
## If you use the same Webpack setup for your server and client bundles
|
28
|
+
If you do use the webpack-dev-server for prerendering, be sure to set the
|
29
|
+
`config/initializers/react_on_rails.rb` setting of
|
30
|
+
|
31
|
+
```
|
32
|
+
config.same_bundle_for_client_and_server = true
|
33
|
+
```
|
34
|
+
|
35
|
+
`dev_server.hmr` maps to [devServer.hot](https://webpack.js.org/configuration/dev-server/#devserverhot).
|
36
|
+
This must be false if you're using the webpack-dev-server for client and server bundles.
|
37
|
+
|
38
|
+
`dev_server.inline` maps to [devServer.inline](https://webpack.js.org/configuration/dev-server/#devserverinline).
|
39
|
+
This must also be false.
|
40
|
+
|
41
|
+
If you don't configure these two to false, you'll see errors like:
|
42
|
+
|
43
|
+
* "ReferenceError: window is not defined" (if hmr is true)
|
44
|
+
* "TypeError: Cannot read property 'prototype' of undefined" (if inline is true)
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
data/docs/basics/i18n.md
CHANGED
@@ -1,23 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# I18n
|
2
2
|
|
3
3
|
Here's a summary of adding the I18n functionality.
|
4
4
|
|
5
|
-
|
5
|
+
1. Add `config.i18n_dir` in `config/initializers/react_on_rails.rb`
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
```js
|
10
|
-
"dependencies": {
|
11
|
-
...
|
12
|
-
"intl": "^1.2.5",
|
13
|
-
"react-intl": "^2.1.5",
|
14
|
-
...
|
15
|
-
}
|
16
|
-
```
|
17
|
-
|
18
|
-
2. Add `config.i18n_dir` in `config/initializers/react_on_rails.rb`
|
19
|
-
|
20
|
-
`react-intl` requires locale files in json format. React on Rails will generate `translations.js` & `default.js` automatically after you configured your `config.i18n_dir` in `config/initializers/react_on_rails.rb`.
|
7
|
+
React on Rails will generate `translations.json` & `default.json` automatically (see #3) after you configured your `config.i18n_dir` in `config/initializers/react_on_rails.rb`.
|
21
8
|
|
22
9
|
```ruby
|
23
10
|
# Replace the following line to the location where you keep translation.js & default.js.
|
@@ -31,16 +18,50 @@ You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/rea
|
|
31
18
|
config.i18n_yml_dir = Rails.root.join("PATH_TO", "YOUR_YAML_I18N_FOLDER")
|
32
19
|
```
|
33
20
|
|
34
|
-
`translations.
|
35
|
-
`default.
|
21
|
+
`translations.json`: All your locales in json format.
|
22
|
+
`default.json`: Default settings in json format.
|
36
23
|
|
37
|
-
|
24
|
+
2. Add `translations.json` and `default.json` to your `.gitignore`.
|
25
|
+
|
26
|
+
3. Javascript locale files must be generated before `yarn build`.
|
38
27
|
|
39
|
-
|
28
|
+
Once you setup `config.i18n_dir` as in the previous step, you will need to make sure `rake react_on_rails:locale` runs before webpack.
|
29
|
+
|
30
|
+
For development, you should adjust your startup scripts (Procfiles) so that they run **`bundle exec rake react_on_rails:locale`** before running any webpack watch process (`yarn run build:development`).
|
31
|
+
|
32
|
+
You may need to configure your CI to run **`bundle exec rake react_on_rails:locale`** before any webpack process if you are not using the React on Rails test helper.
|
33
|
+
|
34
|
+
Note, if you are try to lint before running tests, and you are depending on the test helper to build your locales, your linting will fail because the translations won't be built yet.
|
35
|
+
|
36
|
+
The fix is either to
|
37
|
+
1) run the rake task to build the translations before running the lint command or
|
38
|
+
2) to run the tests first.
|
40
39
|
|
41
|
-
|
40
|
+
# Generate locales with react-intl support
|
42
41
|
|
43
|
-
|
42
|
+
By default the locales generated in json format. If you need to generate files in the prior way
|
43
|
+
with `react-intl` supported via js files:
|
44
|
+
|
45
|
+
1. Specify i18n output format in `react_on_rails.rb`:
|
46
|
+
```rb
|
47
|
+
config.i18n_output_format = 'js'
|
48
|
+
```
|
49
|
+
|
50
|
+
2. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && yarn install`.
|
51
|
+
Versions should be newer than these:
|
52
|
+
|
53
|
+
```js
|
54
|
+
"dependencies": {
|
55
|
+
...
|
56
|
+
"intl": "^1.2.5",
|
57
|
+
"react-intl": "^2.1.5",
|
58
|
+
...
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
3. Add `translations.js` and `default.js` to your `.gitignore` and `.eslintignore`.
|
63
|
+
|
64
|
+
4. In React, you need to initialize `react-intl`, and set parameters for it.
|
44
65
|
|
45
66
|
```js
|
46
67
|
...
|
@@ -73,5 +94,6 @@ You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/rea
|
|
73
94
|
```
|
74
95
|
|
75
96
|
# Notes
|
76
|
-
|
97
|
+
* See why using JSON could be better compare to JS if amount of data is hure [ https://v8.dev/blog/cost-of-javascript-2019#json]( https://v8.dev/blog/cost-of-javascript-2019#json).
|
77
98
|
* See [Support for Rails' i18n pluralization #1000](https://github.com/shakacode/react_on_rails/issues/1000) for a discussion of issues around pluralization.
|
99
|
+
* *Outdated:* You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and [PR #340](https://github.com/shakacode/react-webpack-rails-tutorial/pull/340), [commmited](https://github.com/shakacode/react-webpack-rails-tutorial/commit/ef369ed9d922aea5116ca7e50208169fd7831389) for a complete example.
|
@@ -7,11 +7,11 @@
|
|
7
7
|
1. Add the following to your Gemfile and `bundle install`. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in your `client/package.json` file.
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem "react_on_rails", "
|
11
|
-
gem "webpacker", "~>
|
10
|
+
gem "react_on_rails", "12.0.0" # Update to the current version
|
11
|
+
gem "webpacker", "~> 5"
|
12
12
|
```
|
13
13
|
|
14
|
-
2. Run the following 2 commands to install Webpacker with React. Note,
|
14
|
+
2. Run the following 2 commands to install Webpacker with React. Note, if you are using an older version of Rails than 5.1, you'll need to install webpacker with React per the instructions [here](https://github.com/rails/webpacker).
|
15
15
|
|
16
16
|
|
17
17
|
```bash
|
@@ -45,7 +45,7 @@
|
|
45
45
|
|
46
46
|
## Installation
|
47
47
|
|
48
|
-
See the [Installation Overview](
|
48
|
+
See the [Installation Overview](docs/outdated/manual-installation-overview.md) for a concise set summary of what's in a React on Rails installation.
|
49
49
|
|
50
50
|
|
51
51
|
## NPM
|
@@ -57,8 +57,3 @@ $ yarn add react-on-rails --exact
|
|
57
57
|
```
|
58
58
|
|
59
59
|
That will install the latest version and update your package.json. **NOTE:** the `--exact` flag will ensure that you do not have a "~" or "^" for your react-on-rails version in your package.json.
|
60
|
-
|
61
|
-
## Webpacker Configuration
|
62
|
-
|
63
|
-
React on Rails users should set configuration value `compile` to false, as React on Rails handles compilation for test and production environments.
|
64
|
-
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Minitest Configuration
|
2
|
+
|
3
|
+
The setup for minitest is the same as for rspec with the following difference.
|
4
|
+
|
5
|
+
Rather than calling `ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)`, instead you will do something like this:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class ActiveSupport::TestCase
|
9
|
+
setup do
|
10
|
+
ReactOnRails::TestHelper.ensure_assets_compiled
|
11
|
+
end
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
Or maybe something like this, from the [minitest docs](https://github.com/seattlerb/minitest/blob/master/lib/minitest/test.rb#L119):
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
module MyMinitestPlugin
|
20
|
+
def before_setup
|
21
|
+
super
|
22
|
+
ReactOnRails::TestHelper.ensure_assets_compiled
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MiniTest::Test
|
27
|
+
include MyMinitestPlugin
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
|
@@ -10,7 +10,7 @@ During the Rails rendering of HTML per a browser request, the Rails server will
|
|
10
10
|
|
11
11
|
The default JavaScript interpretter is [ExecJS](https://github.com/rails/execjs). If you want to maximize the perfomance of your server rendering, then you want to use React on Rails Pro which uses NodeJS to do the server rendering. See the [docs for React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
|
12
12
|
|
13
|
-
See [this note](
|
13
|
+
See [this note](docs/outdated/how-react-on-rails-works.md#client-side-rendering-vs-server-side-rendering)
|
14
14
|
|
15
15
|
|
16
16
|
## How do you do Server Rendering with React on Rails?
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# Recommended Project structure
|
2
2
|
|
3
|
-
While React On Rails does not *enforce* a specific project structure, we do *recommend* a standard organization. The more we follow standards as a community, the easier it will be for all of us to move between various Rails projects that include React On Rails.
|
4
|
-
|
5
3
|
The React on Rails generator uses the standard `rails/webpacker` convention of this structure:
|
6
4
|
|
7
5
|
```yml
|
@@ -14,21 +12,9 @@ app/javascript:
|
|
14
12
|
│ └── hello-world-bundle.js
|
15
13
|
```
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
1. No support for different entry points for server rendering.
|
20
|
-
2. Webpacker adds an extra layer of abstraction over Webpack, which you probably don't want.
|
21
|
-
|
22
|
-
This default rails/webpacker configuration is used for the generator because:
|
23
|
-
|
24
|
-
1. Minimizes the amount of generated code to get up and running with React on Rails.
|
25
|
-
2. Good enough for very simple projects.
|
26
|
-
3. Configuration of Webpack is not the goal of this library, React on Rails.
|
15
|
+
However, you may wish to move all your client side files to a single directory called something like `/client`.
|
27
16
|
|
28
|
-
|
29
|
-
Thus, the generator structure and using rails/webpacker for Webpack configuration **is not recommended** for any commercial projects, especially those that will use server rendering. Instead, the recommended structure is shown in this example app: [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and described below.
|
30
|
-
|
31
|
-
## Steps to convert from the generator defaults to use the recommended `/client` directory structure.
|
17
|
+
## Steps to convert from the generator defaults to use a `/client` directory structure.
|
32
18
|
|
33
19
|
1. Move the directory:
|
34
20
|
|
@@ -44,7 +30,7 @@ mv app/javascript client
|
|
44
30
|
|
45
31
|
## Moving node_modules from `/` to `/client` with a custom webpack setup.
|
46
32
|
|
47
|
-
`rails/webpacker` probably doesn't support having your main node_modules directory under `/client`, so only follow these steps if you want to use your own webpack configuration
|
33
|
+
`rails/webpacker` probably doesn't support having your main node_modules directory under `/client`, so only follow these steps if you want to use your own webpack configuration.
|
48
34
|
|
49
35
|
1. Move the `/package.json` to `/client/package.json`
|
50
36
|
2. Create a `/package.json` that delegates to `/client/package.json`. See the example in [spec/dummy/package.json](../../spec/dummy/package.json).
|
@@ -57,7 +43,7 @@ mv app/javascript client
|
|
57
43
|
1. `/client/app/bundles`: Top level of different app domains. Use a name within this directory for you app domains. For example, if you had a domain called `widget-editing`, then you would have: `/client/app/bundles/widget-editing`
|
58
44
|
1. `/client/app/lib`: Common code for bundles
|
59
45
|
1. Within each bundle directory (or the lib directory), such as a domain named "comments"
|
60
|
-
`/client/app/bundles/comments`, use following directory structure:
|
46
|
+
`/client/app/bundles/comments`, use following directory structure, if you're using redux. However, with React hooks, this will probably be a bit different:
|
61
47
|
|
62
48
|
* `/actions`: Redux actions.
|
63
49
|
* `/components`: "dumb" components (no connections to Redux or Ajax). These get props and can render themselves and children.
|
@@ -79,7 +65,7 @@ This isn't really any technique, as you keep handling all your styling assets us
|
|
79
65
|
1. Much simpler! There's no changes really from your current processes.
|
80
66
|
|
81
67
|
### Using Webpack to Manage Styling Assets
|
82
|
-
This technique involves customization of the webpack config files to generate CSS, image, and font assets.
|
68
|
+
This technique involves customization of the webpack config files to generate CSS, image, and font assets.
|
83
69
|
|
84
70
|
#### Directory structure
|
85
71
|
1. `/client/app/assets`: Assets for CSS for client app.
|
@@ -89,6 +75,3 @@ This technique involves customization of the webpack config files to generate CS
|
|
89
75
|
1. You can use [CSS modules](https://github.com/css-modules/css-modules), which is super compelling once you seen the benefits.
|
90
76
|
1. You can do hot reloading of your assets. Thus, you do not have to refresh your web page to see asset change, including changing styles.
|
91
77
|
1. You can run your client code on a mocked out express server for super fast prototyping. In other words, your client application can somewhat more easily be move to a different application server.
|
92
|
-
|
93
|
-
#### Updates 2017-03-04 Regarding CSS handled by Webpack
|
94
|
-
* See article [Best practices for CSS and CSS Modules using Webpack](https://forum.shakacode.com/t/best-practices-for-css-and-css-modules-using-webpack/799).
|
data/docs/basics/{generator-functions-and-railscontext.md → render-functions-and-railscontext.md}
RENAMED
@@ -1,45 +1,80 @@
|
|
1
|
-
#
|
1
|
+
# Render-Functions and the Rails Context
|
2
2
|
|
3
|
-
##
|
3
|
+
## Render-Functions
|
4
4
|
|
5
|
-
When you use a
|
5
|
+
When you use a render-function to create react components (or renderedHtml on the server), or you
|
6
|
+
used shared redux stores, you get two params passed to your function that creates a React component:
|
6
7
|
|
7
8
|
1. `props`: Props that you pass in the view helper of either `react_component` or `redux_store`
|
8
|
-
2. `railsContext`: Rails contextual information, such as the current pathname. You can customize
|
9
|
+
2. `railsContext`: Rails contextual information, such as the current pathname. You can customize
|
10
|
+
this in your config file. **Note**: The `railsContext` is not related to the concept of a
|
11
|
+
["context" for React components](https://facebook.github.io/react/docs/context.html#how-to-use-context).
|
9
12
|
|
10
|
-
|
13
|
+
These parameters (`props` and `railsContext`) will be the same regardless of either client or server
|
14
|
+
side rendering, except for the key `serverSide` based on whether or not you are server rendering.
|
11
15
|
|
12
|
-
While you could manually configure your Rails code to pass the "`railsContext` information" with
|
16
|
+
While you could manually configure your Rails code to pass the "`railsContext` information" with
|
17
|
+
the rest of your "props", the `railsContext` is a convenience because it's passed consistently to
|
18
|
+
all invocations of render functions.
|
13
19
|
|
14
|
-
For example, suppose you create a "
|
20
|
+
For example, suppose you create a "render-function" called MyAppComponent.
|
15
21
|
|
16
22
|
```js
|
17
23
|
import React from 'react';
|
18
24
|
const MyAppComponent = (props, railsContext) => (
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
25
|
+
// NOTE: need to wrap in a function so this is proper React function component that can use
|
26
|
+
// hooks
|
27
|
+
|
28
|
+
// the props get passed again, but we ignore since we use a closure
|
29
|
+
// or should we
|
30
|
+
() =>
|
31
|
+
<div>
|
32
|
+
<p>props are: {JSON.stringify(props)}</p>
|
33
|
+
<p>railsContext is: {JSON.stringify(railsContext)}
|
34
|
+
</p>
|
35
|
+
</div>
|
24
36
|
);
|
25
37
|
export default MyAppComponent;
|
26
38
|
```
|
27
39
|
|
40
|
+
------------------------------
|
41
|
+
|
42
|
+
_This would be alternate API where you have to call React.createElement and the React on Rails code doesn't do that._
|
43
|
+
|
44
|
+
```js
|
45
|
+
import React from 'react';
|
46
|
+
const MyAppComponent = (props, railsContext) => (
|
47
|
+
// NOTE: need to wrap in a function so this is proper React function component that can use
|
48
|
+
// hooks
|
49
|
+
React.createElement(
|
50
|
+
() =>
|
51
|
+
<div>
|
52
|
+
<p>props are: {JSON.stringify(props)}</p>
|
53
|
+
<p>railsContext is: {JSON.stringify(railsContext)}
|
54
|
+
</p>
|
55
|
+
</div>,
|
56
|
+
props)
|
57
|
+
);
|
58
|
+
export default MyAppComponent;
|
59
|
+
```
|
60
|
+
|
61
|
+
------------------------------------
|
62
|
+
|
28
63
|
*Note: you will get a React browser console warning if you try to serverRender this since the value of `serverSide` will be different for server rendering.*
|
29
64
|
|
30
|
-
So if you register your
|
65
|
+
So if you register your render-function `MyAppComponent`, it will get called like:
|
31
66
|
|
32
67
|
```js
|
33
68
|
reactComponent = MyAppComponent(props, railsContext);
|
34
69
|
```
|
35
70
|
|
36
|
-
and, similarly, any redux store always initialized with 2 parameters:
|
71
|
+
and, similarly, any redux store is always initialized with 2 parameters:
|
37
72
|
|
38
73
|
```js
|
39
74
|
reduxStore = MyReduxStore(props, railsContext);
|
40
75
|
```
|
41
76
|
|
42
|
-
Note: you never make these calls. React on Rails makes these calls when it does either client or server rendering. You will define functions that take these 2 params and return a React component or a Redux Store. Naturally, you do not have to use second parameter of the railsContext if you do not need it.
|
77
|
+
Note: you never make these calls. React on Rails makes these calls when it does either client or server rendering. You will define functions that take these 2 params and return a React component or a Redux Store. Naturally, you do not have to use second parameter of the railsContext if you do not need it. If you don't take a second parameter, then you're probably defining a React function component and you will simply return a React Element, often just JSX.
|
43
78
|
|
44
79
|
(Note: see below [section](#multiple-react-components-on-a-page-with-one-store) on how to setup redux stores that allow multiple components to talk to the same store.)
|
45
80
|
|
@@ -76,7 +111,7 @@ Plus, you can add your customizations to this. See "rendering extension" below.
|
|
76
111
|
|
77
112
|
## Rails Context
|
78
113
|
|
79
|
-
The `railsContext` is a second param passed to your
|
114
|
+
The `railsContext` is a second param passed to your render-functions for React components. This is in addition to the props that are passed from the `react_component` Rails helper. For example:
|
80
115
|
|
81
116
|
ERB view file:
|
82
117
|
|
@@ -91,7 +126,8 @@ This is what your HelloWorld.js file might contain. The railsContext is always a
|
|
91
126
|
import React from 'react';
|
92
127
|
|
93
128
|
export default (props, railsContext) => {
|
94
|
-
|
129
|
+
// Note, wrap in a function so this is React function component
|
130
|
+
return () => (
|
95
131
|
<div>
|
96
132
|
Your locale is {railsContext.i18nLocale}.<br/>
|
97
133
|
Hello, {props.name}!
|
@@ -100,15 +136,17 @@ export default (props, railsContext) => {
|
|
100
136
|
};
|
101
137
|
```
|
102
138
|
|
103
|
-
## Why is the railsContext
|
139
|
+
## Why is the railsContext only passed to render-functions?
|
104
140
|
|
105
|
-
There's no reason that the railsContext would ever get passed to your React component unless the value is explicitly put into the props used for rendering. If you create a react component, rather than a
|
141
|
+
There's no reason that the railsContext would ever get passed to your React component unless the value is explicitly put into the props used for rendering. If you create a react component, rather than a render-function, for use by React on Rails, then you get whatever props are passed in from the view helper, which **does not include the Rails Context**. It's trivial to wrap your component in a "render-function" to return a new component that takes both:
|
106
142
|
|
107
143
|
```js
|
108
144
|
import React from 'react';
|
109
145
|
import AppComponent from './AppComponent';
|
110
146
|
const AppComponentWithRailsContext = (props, railsContext) => (
|
111
|
-
|
147
|
+
// Create a React Function Component so you can
|
148
|
+
// use the React Hooks API in this React Function Component
|
149
|
+
() => <AppComponent {...{...props, railsContext}}/>
|
112
150
|
)
|
113
151
|
export default AppComponentWithRailsContext;
|
114
152
|
```
|
@@ -153,7 +191,7 @@ See [spec/dummy/config/initializers/react_on_rails.rb](https://github.com/shakac
|
|
153
191
|
module RenderingExtension
|
154
192
|
|
155
193
|
# Return a Hash that contains custom values from the view context that will get merged with
|
156
|
-
# the standard rails_context values and passed to all calls to
|
194
|
+
# the standard rails_context values and passed to all calls to render-functions used by the
|
157
195
|
# react_component and redux_store view helpers
|
158
196
|
def self.custom_context(view_context)
|
159
197
|
{
|