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
Refactor ESLint configuration to simplify default project settings and enhance documentation on render functions, clarifying parameter usage and return types.
Copy file name to clipboardExpand all lines: docs/javascript/render-functions.md
+42-8Lines changed: 42 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,46 @@ This guide explains how render functions work in React on Rails and how to use t
4
4
5
5
## Types of Render Functions and Their Return Values
6
6
7
+
Render functions take two parameters:
8
+
9
+
1.`props`: The props passed from the Ruby helper methods (via the `props:` parameter), which become available in your JavaScript.
10
+
2.`railsContext`: Rails contextual information like current pathname, locale, etc. See the [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) documentation for more details.
11
+
12
+
### Identifying Render Functions
13
+
14
+
React on Rails needs to identify which functions are render functions (as opposed to regular React components). There are two ways to mark a function as a render function:
15
+
16
+
1. Accept two parameters in your function definition: `(props, railsContext)` - React on Rails will detect this signature
17
+
2. Add a `renderFunction = true` property to your function - This is useful when your function doesn't need the railsContext
> React on Rails will not handle the actual redirection to another page. It will just return an empty component and depend on the front end to handle the redirection when it renders the router on the front end.
111
+
> React on Rails does not perform actual page redirections. Instead, it returns an empty component and relies on the front end to handle the redirection when the router is rendered. The `redirectLocation` property is logged in the console and ignored by the server renderer. If the `routeError` property is not null or undefined, it is logged and will cause Ruby to throw a `ReactOnRails::PrerenderError` if the `raise_on_prerender_error` configuration is enabled.
@@ -93,7 +127,7 @@ Take a look at [serverRenderReactComponent.test.ts](https://github.com/shakacode
93
127
94
128
2.**Objects Require Specific Properties** - Non-promise objects must include a `renderedHtml` property to be valid when used with `react_component`.
95
129
96
-
3.**Async Functions Support All Return Types** - Async functions can return React components, strings, or objects with any property structure due to special handling in the server renderer, but it doesn't support properties like `redirectLocation` and `routingError` that can be returned by sync render function. See [7. Redirect Information](#7-redirect-information).
130
+
3.**Async Functions Support All Return Types** - Async functions can return React components, strings, or objects with any property structure due to special handling in the server renderer, but it doesn't support properties like `redirectLocation` and `routeError` that can be returned by sync render function. See [7. Redirect Information](#7-redirect-information).
97
131
98
132
## Ruby Helper Functions
99
133
@@ -139,7 +173,7 @@ The `react_component_hash` helper is used when your render function returns an o
139
173
</div>
140
174
```
141
175
142
-
This helper accepts render functions that return objects with a `renderedHtml` property containing `componentHtml` and any other necessary properties. It also supports promises that resolve to Server-Side hash.
176
+
This helper accepts render functions that return objects with a `renderedHtml` property containing `componentHtml` and any other necessary properties. It also supports promises that resolve to a server-side hash.
0 commit comments