Skip to content

Commit e827e86

Browse files
Refactor ESLint configuration to simplify default project settings and enhance documentation on render functions, clarifying parameter usage and return types.
1 parent f76f1e5 commit e827e86

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

docs/javascript/render-functions.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,46 @@ This guide explains how render functions work in React on Rails and how to use t
44

55
## Types of Render Functions and Their Return Values
66

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
18+
19+
```jsx
20+
// Method 1: Use signature with two parameters
21+
const MyComponent = (props, railsContext) => {
22+
return () => (
23+
<div>
24+
Hello {props.name} from {railsContext.pathname}
25+
</div>
26+
);
27+
};
28+
29+
// Method 2: Use renderFunction property
30+
const MyOtherComponent = (props) => {
31+
return () => <div>Hello {props.name}</div>;
32+
};
33+
MyOtherComponent.renderFunction = true;
34+
35+
ReactOnRails.register({ MyComponent, MyOtherComponent });
36+
```
37+
738
Render functions can return several types of values:
839

940
### 1. React Components
1041

1142
```jsx
1243
const MyComponent = (props, _railsContext) => {
13-
return () => <div>Hello {props.name}</div>;
44+
// The `props` parameter here is identical to the `props` passed from the Ruby helper methods (via the `props:` parameter).
45+
// Both `props` and `reactProps` refer to the same object.
46+
return (reactProps) => <div>Hello {props.name}</div>;
1447
};
1548
```
1649

@@ -27,7 +60,7 @@ const MyComponent = (props, _railsContext) => {
2760
};
2861
```
2962

30-
### 3. Objects with `renderedHtml` as object containing `componentHtml` and other properties if needed (Server-Side hash)
63+
### 3. Objects with `renderedHtml` as object containing `componentHtml` and other properties if needed (server-side hash)
3164

3265
```jsx
3366
const MyComponent = (props, _railsContext) => {
@@ -50,7 +83,7 @@ const MyComponent = async (props, _railsContext) => {
5083
};
5184
```
5285

53-
### 5. Promises of Server-Side hash
86+
### 5. Promises of server-side hash
5487

5588
```jsx
5689
const MyComponent = async (props, _railsContext) => {
@@ -75,12 +108,13 @@ const MyComponent = async (props, _railsContext) => {
75108
### 7. Redirect Information
76109

77110
> [!NOTE]
78-
> 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.
79112
80113
```jsx
81114
const MyComponent = (props, _railsContext) => {
82115
return {
83116
redirectLocation: { pathname: '/new-path', search: '' },
117+
routeError: null,
84118
};
85119
};
86120
```
@@ -93,7 +127,7 @@ Take a look at [serverRenderReactComponent.test.ts](https://github.com/shakacode
93127

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

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).
97131

98132
## Ruby Helper Functions
99133

@@ -139,7 +173,7 @@ The `react_component_hash` helper is used when your render function returns an o
139173
</div>
140174
```
141175

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.
143177

144178
#### When to use:
145179

@@ -187,7 +221,7 @@ ReactOnRails.register({ RenderedHtmlComponent });
187221
<%= react_component("RenderedHtmlComponent", props: { name: "John" }) %>
188222
```
189223

190-
### Return Type 3: Object with Server-Side hash
224+
### Return Type 3: Object with server-side hash
191225

192226
```jsx
193227
const HelmetComponent = (props) => {
@@ -237,7 +271,7 @@ ReactOnRails.register({ AsyncStringComponent });
237271
<%= react_component("AsyncStringComponent", props: { dataUrl: "/api/data" }) %>
238272
```
239273

240-
### Return Type 5: Promise of Server-Side hash
274+
### Return Type 5: Promise of server-side hash
241275

242276
```jsx
243277
const AsyncObjectComponent = async (props) => {

eslint.config.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ const config = tsEslint.config([
145145
languageOptions: {
146146
parserOptions: {
147147
projectService: {
148-
allowDefaultProject: [
149-
'eslint.config.ts',
150-
'knip.ts',
151-
'node_package/tests/serverRenderReactComponent.test.ts',
152-
],
148+
allowDefaultProject: ['eslint.config.ts', 'knip.ts', 'node_package/tests/*.test.ts'],
153149
// Needed because `import * as ... from` instead of `import ... from` doesn't work in this file
154150
// for some imports.
155151
defaultProject: 'tsconfig.eslint.json',

0 commit comments

Comments
 (0)