You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/render-functions-and-railscontext.md
+18-15Lines changed: 18 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,8 +23,7 @@ For example, suppose you create a "render-function" called MyAppComponent.
23
23
importReactfrom'react';
24
24
constMyAppComponent=
25
25
(props, railsContext) =>
26
-
// NOTE: need to wrap in a function so this is proper React function component that can use
27
-
// hooks
26
+
// NOTE: need to wrap in a function so this is a proper React function component that can use hooks
28
27
29
28
// the props get passed again, but we ignore since we use a closure
30
29
// or should we
@@ -39,7 +38,7 @@ export default MyAppComponent;
39
38
40
39
---
41
40
42
-
_This would be an alternate API where you have to call React.`createElement` and the React on Rails code doesn't do that._
41
+
_This would be an alternate API where you have to call `React.createElement` and the React on Rails code doesn't do that._
43
42
44
43
```js
45
44
importReactfrom'react';
@@ -60,7 +59,8 @@ export default MyAppComponent;
60
59
61
60
---
62
61
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._
62
+
> [!NOTE]
63
+
> You will get a React browser console warning if you try to render this on the server since the value of `serverSide` will be different for server rendering.
64
64
65
65
So if you register your render-function `MyAppComponent`, it will get called like:
66
66
@@ -74,21 +74,23 @@ Similarly, any Redux store is always initialized with 2 parameters:
74
74
reduxStore =MyReduxStore(props, railsContext);
75
75
```
76
76
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.
77
+
> [!NOTE]
78
+
> 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.
78
79
79
-
(Note: see below [section](#multiple-react-components-on-a-page-with-one-store) on how to set up Redux stores that allow multiple components to talk to the same store.)
80
+
> [!NOTE]
81
+
> See [Redux Store](https://www.shakacode.com/react-on-rails/docs/api/redux-store-api/#multiple-react-components-on-a-page-with-one-store) on how to set up Redux stores that allow multiple components to talk to the same store.
80
82
81
-
The `railsContext` has: (see implementation in file[ReactOnRails::Helper](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/helper.rb), method `rails_context` for the definitive list).
83
+
The `railsContext` has: (see the implementation in [ReactOnRails::Helper](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/helper.rb), method `rails_context` for the definitive list).
82
84
83
85
```ruby
84
86
{
85
-
railsEnv:Rails.env
87
+
railsEnv:Rails.env,
86
88
inMailer: in_mailer?,
87
89
# Locale settings
88
90
i18nLocale:I18n.locale,
89
91
i18nDefaultLocale:I18n.default_locale,
90
92
rorVersion:ReactOnRails::VERSION,
91
-
rorPro:ReactOnRails::Utils.react_on_rails_pro?
93
+
rorPro:ReactOnRails::Utils.react_on_rails_pro?,
92
94
93
95
# URL settings
94
96
href: request.original_url,
@@ -98,10 +100,11 @@ The `railsContext` has: (see implementation in file [ReactOnRails::Helper](https
The outer `{...` is for the [JSX spread operator for attributes](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes) and the inner `{...` is for the [Spread in object literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Spread_in_object_literals).
163
+
The outer `{...` is for the [JSX spread operator for attributes](https://legacy.reactjs.org/docs/jsx-in-depth.html#spread-attributes) and the inner `{...` is for the [Spread in object literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Spread_in_object_literals).
161
164
162
165
## Use Cases
163
166
@@ -173,7 +176,7 @@ Suppose you want to display a nav bar with the current navigation link highlight
173
176
174
177
Suppose you want to turn off animation when doing server side rendering. The `serverSide` value is just what you need.
175
178
176
-
## Customization of the rails_context
179
+
## Customization of the Rails context
177
180
178
181
You can customize the values passed in the `railsContext` in your `config/initializers/react_on_rails.rb`. Here's how.
179
182
@@ -201,4 +204,4 @@ module RenderingExtension
201
204
end
202
205
```
203
206
204
-
In this case, a prop and value for `somethingUseful` will go into the railsContext passed to all react_component and redux_store calls. You may set any values available in the view rendering context.
207
+
In this case, a prop and value for `somethingUseful` will go into the `railsContext` passed to all `react_component` and `redux_store` calls. You may set any values available in the view rendering context.
0 commit comments