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/javascript/render-functions.md
+74-58Lines changed: 74 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,21 @@
1
1
# React on Rails Render Functions: Usage Guide
2
2
3
-
Based on the test file `serverRenderReactComponent.test.ts` and the existing documentation, I'll clarify how render functions work in React on Rails and how to use them with each Ruby helper method.
3
+
This guide explains how render functions work in React on Rails and how to use them with Ruby helper methods.
4
4
5
5
## Types of Render Functions and Their Return Values
6
6
7
-
Looking at the test file, render functions can return several types of values:
7
+
Render functions can return several types of values:
8
8
9
-
### 1. React Components (JSX)
9
+
### 1. React Components
10
10
11
11
```jsx
12
12
constMyComponent= (props, _railsContext) => {
13
-
return<div>Hello {props.name}</div>;
13
+
return() =><div>Hello {props.name}</div>;
14
14
};
15
15
```
16
16
17
+
> [!NOTE] Ensure to return a React component (a function or class) and not a React element (the result of calling `React.createElement` or JSX).
> [!NOTE] 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.
Based on tests in [serverRenderReactComponent.test.ts](https://github.com/shakacode/react_on_rails/blob/master/node_package/tests/serverRenderReactComponent.test.ts):
74
89
75
-
1.**Direct String Returns Don't Work** - Returning a raw HTML string directly from a render function causes an error. The test `doesn't render html string returned directly from render function` demonstrates this.
90
+
1.**Direct String Returns Don't Work** - Returning a raw HTML string directly from a render function causes an error. Always wrap HTML strings in `{ renderedHtml: '...' }`.
76
91
77
-
2.**Non-Promise Objects Need renderedHtml** - Non-promise objects must include a `renderedHtml` property to be valid, as shown in the test `doesn't render object without renderedHtml property`.
92
+
2.**Objects Require Specific Properties** - Non-promise objects must include a `renderedHtml` property to be valid when used with `react_component`.
78
93
79
-
3.**Async Functions Have Different Behavior** - Interestingly, the test `returns the object returned by async render function even if it doesn't have renderedHtml property` shows that Promise-returning functions can return objects without a `renderedHtml` property and they will still work.
94
+
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.
80
95
81
-
## Ruby Helper Functions with Render Functions
96
+
## Ruby Helper Functions
82
97
83
98
### 1. react_component
84
99
85
-
The `react_component` helper is used for rendering a single React component. It accepts various return types from render functions:
100
+
The `react_component` helper renders a single React component in your view.
This helper accepts render functions that return React components, objects with a `renderedHtml` property, or promises that resolve to React components, or strings.
107
+
101
108
#### When to use:
102
109
103
110
- When you need to render a single component
@@ -107,7 +114,7 @@ The `react_component` helper is used for rendering a single React component. It
107
114
#### Not suitable for:
108
115
109
116
- When your render function returns an object with multiple HTML strings
110
-
- When you need to insert content in different parts of the page
117
+
- When you need to insert content in different parts of the page, such as meta tags & style tags
111
118
112
119
### 2. react_component_hash
113
120
@@ -130,6 +137,8 @@ The `react_component_hash` helper is used when your render function returns an o
130
137
</div>
131
138
```
132
139
140
+
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 objects with `componentHtml` and other properties if needed.
141
+
133
142
#### When to use:
134
143
135
144
- When your render function returns multiple HTML strings in an object
@@ -148,55 +157,47 @@ The `react_component_hash` helper is used when your render function returns an o
148
157
- The object MUST include a `componentHtml` key
149
158
- All other keys are optional and can be accessed in your Rails view
150
159
151
-
## Best Practices Based on the Tests
152
-
153
-
1.**Always Use Objects for Multiple HTML Parts**: If you need multiple HTML strings, return an object with named properties, and use `react_component_hash` to access them.
154
-
155
-
2.**Don't Return Raw HTML Strings**: Tests show that returning a raw HTML string directly causes errors - either use JSX or wrap in a `{ renderedHtml: '...' }` object.
156
-
157
-
3.**Async Functions Work with Objects**: For async render functions, you can return both strings and objects (with or without a `renderedHtml` property).
158
-
159
-
4.**Use Redirect Object Format**: For redirects, return an object with `{ redirectLocation, error, renderedHtml: null }`.
160
-
161
-
## Example: Different Return Types with Appropriate Helper Methods
By understanding the different return types and which helper to use with each, you can create sophisticated server-rendered React components that fully integrate with your Rails views.
308
+
By understanding these return types and which helper to use with each, you can create sophisticated server-rendered React components that fully integrate with your Rails views.
0 commit comments