Skip to content

Commit 9caf8a5

Browse files
Update documentation for render functions to clarify property names and improve formatting
1 parent 4e8d41c commit 9caf8a5

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

docs/javascript/render-functions.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const MyComponent = (props, _railsContext) => {
1616

1717
> [!NOTE] Ensure to return a React component (a function or class) and not a React element (the result of calling `React.createElement` or JSX).
1818
19-
### 2. Objects with renderedHtml Property
19+
### 2. Objects with `renderedHtml` string property
2020

2121
```jsx
2222
const MyComponent = (props, _railsContext) => {
@@ -26,7 +26,7 @@ const MyComponent = (props, _railsContext) => {
2626
};
2727
```
2828

29-
### 3. Objects with renderedHtml as object containing componentHtml and other properties if needed
29+
### 3. Objects with `renderedHtml` as object containing `componentHtml` and other properties if needed (Server-Side hash)
3030

3131
```jsx
3232
const MyComponent = (props, _railsContext) => {
@@ -49,7 +49,7 @@ const MyComponent = async (props, _railsContext) => {
4949
};
5050
```
5151

52-
### 5. Promises of Objects containing componentHtml and other properties if needed
52+
### 5. Promises of Server-Side hash
5353

5454
```jsx
5555
const MyComponent = async (props, _railsContext) => {
@@ -73,7 +73,8 @@ const MyComponent = async (props, _railsContext) => {
7373

7474
### 7. Redirect Information
7575

76-
> [!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.
76+
> [!NOTE]
77+
> 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.
7778
7879
```jsx
7980
const MyComponent = (props, _railsContext) => {
@@ -85,13 +86,13 @@ const MyComponent = (props, _railsContext) => {
8586

8687
## Important Rendering Behavior
8788

88-
Based on tests in [serverRenderReactComponent.test.ts](https://github.com/shakacode/react_on_rails/blob/master/node_package/tests/serverRenderReactComponent.test.ts):
89+
Take a look at [serverRenderReactComponent.test.ts](https://github.com/shakacode/react_on_rails/blob/master/node_package/tests/serverRenderReactComponent.test.ts):
8990

9091
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: '...' }`.
9192

9293
2. **Objects Require Specific Properties** - Non-promise objects must include a `renderedHtml` property to be valid when used with `react_component`.
9394

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.
95+
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).
9596

9697
## Ruby Helper Functions
9798

@@ -137,7 +138,7 @@ The `react_component_hash` helper is used when your render function returns an o
137138
</div>
138139
```
139140

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+
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.
141142

142143
#### When to use:
143144

@@ -162,7 +163,6 @@ This helper accepts render functions that return objects with a `renderedHtml` p
162163
### Return Type 1: React Component
163164

164165
```jsx
165-
// JavaScript
166166
const SimpleComponent = (props, _railsContext) => () => <div>Hello {props.name}</div>;
167167
ReactOnRails.register({ SimpleComponent });
168168
```
@@ -175,7 +175,6 @@ ReactOnRails.register({ SimpleComponent });
175175
### Return Type 2: Object with renderedHtml
176176

177177
```jsx
178-
// JavaScript
179178
const RenderedHtmlComponent = (props, _railsContext) => {
180179
return { renderedHtml: `<div>Hello ${props.name}</div>` };
181180
};
@@ -187,10 +186,9 @@ ReactOnRails.register({ RenderedHtmlComponent });
187186
<%= react_component("RenderedHtmlComponent", props: { name: "John" }) %>
188187
```
189188

190-
### Return Type 3: Object with componentHtml and other properties if needed
189+
### Return Type 3: Object with Server-Side hash
191190

192191
```jsx
193-
// JavaScript
194192
const HelmetComponent = (props) => {
195193
return {
196194
renderedHtml: {
@@ -200,6 +198,9 @@ const HelmetComponent = (props) => {
200198
},
201199
};
202200
};
201+
// The render function should either:
202+
// 1. Accept two arguments: (props, railsContext)
203+
// 2. Have a property `renderFunction` set to true
203204
HelmetComponent.renderFunction = true;
204205
ReactOnRails.register({ HelmetComponent });
205206
```
@@ -222,7 +223,6 @@ ReactOnRails.register({ HelmetComponent });
222223
### Return Type 4: Promise of String
223224

224225
```jsx
225-
// JavaScript
226226
const AsyncStringComponent = async (props) => {
227227
const data = await fetchData();
228228
return `<div>Hello ${data.name}</div>`;
@@ -236,10 +236,9 @@ ReactOnRails.register({ AsyncStringComponent });
236236
<%= react_component("AsyncStringComponent", props: { dataUrl: "/api/data" }) %>
237237
```
238238

239-
### Return Type 5: Promise of Object containing componentHtml and other properties if needed
239+
### Return Type 5: Promise of Server-Side hash
240240

241241
```jsx
242-
// JavaScript
243242
const AsyncObjectComponent = async (props) => {
244243
const data = await fetchData();
245244
return {
@@ -269,7 +268,6 @@ ReactOnRails.register({ AsyncObjectComponent });
269268
### Return Type 6: Promise of React Component
270269

271270
```jsx
272-
// JavaScript
273271
const AsyncReactComponent = async (props) => {
274272
const data = await fetchData();
275273
return () => <div>Hello {data.name}</div>;

0 commit comments

Comments
 (0)