Skip to content

Commit cdbdc86

Browse files
move some comments to another place
1 parent 45b61ba commit cdbdc86

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

node_package/src/serverRenderReactComponent.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function processServerRenderHash(result: ServerRenderResult, options: RenderOpti
4242
console.log(`ROUTER REDIRECT: ${options.componentName} to dom node with id: ${options.domNodeId}, redirect to ${redirectPath}`);
4343
}
4444
// For redirects on server rendering, we can't stop Rails from returning the same result.
45-
// Possibly, someday, we could have the rails server redirect.
45+
// Possibly, someday, we could have the Rails server redirect.
4646
htmlResult = '';
4747
} else {
4848
htmlResult = result.renderedHtml as string;
@@ -51,14 +51,14 @@ function processServerRenderHash(result: ServerRenderResult, options: RenderOpti
5151
return { result: htmlResult, hasErrors };
5252
}
5353

54-
function processPromise(result: Promise<unknown>, renderingReturnsPromises: boolean): Promise<string> | string {
54+
function processPromise(result: Promise<string>, renderingReturnsPromises: boolean): Promise<string> | string {
5555
if (!renderingReturnsPromises) {
5656
console.error('Your render function returned a Promise, which is only supported by a node renderer, not ExecJS.');
5757
// If the app is using server rendering with ExecJS, then the promise will not be awaited.
5858
// And when a promise is passed to JSON.stringify, it will be converted to '{}'.
5959
return '{}';
6060
}
61-
return result as Promise<string>;
61+
return result;
6262
}
6363

6464
function processReactElement(result: ReactElement): string {

node_package/src/types/index.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ type CreateReactOutputResult = ServerRenderResult | ReactElement | Promise<strin
4343

4444
type RenderFunctionResult = ReactComponent | ServerRenderResult | Promise<string>;
4545

46+
/**
47+
* Render functions are used to create dynamic React components or server-rendered HTML with side effects.
48+
* They receive two arguments: props and railsContext.
49+
*
50+
* @param props - The component props passed to the render function
51+
* @param railsContext - The Rails context object containing environment information
52+
* @returns A string, React component, React element, or a Promise resolving to a string
53+
*
54+
* @remarks
55+
* To distinguish a render function from a React Function Component:
56+
* 1. Ensure it accepts two parameters (props and railsContext), even if railsContext is unused, or
57+
* 2. Set the `renderFunction` property to `true` on the function object.
58+
*
59+
* If neither condition is met, it will be treated as a React Function Component,
60+
* and ReactDOMServer will attempt to render it.
61+
*
62+
* @example
63+
* // Option 1: Two-parameter function
64+
* const renderFunction = (props, railsContext) => { ... };
65+
*
66+
* // Option 2: Using renderFunction property
67+
* const anotherRenderFunction = (props) => { ... };
68+
* anotherRenderFunction.renderFunction = true;
69+
*/
4670
interface RenderFunction {
4771
(props?: any, railsContext?: RailsContext, domNodeId?: string): RenderFunctionResult;
4872
// We allow specifying that the function is RenderFunction and not a React Function Component
@@ -67,10 +91,10 @@ export type { // eslint-disable-line import/prefer-default-export
6791
export interface RegisteredComponent {
6892
name: string;
6993
component: ReactComponentOrRenderFunction;
70-
// Indicates if the registered component is a Render-function.
71-
// Render-functions receive two arguments: props and railsContext.
72-
// They should return a string, React component, React element, or a Promise.
73-
// These functions are used to create dynamic React components or server-rendered HTML.
94+
/**
95+
* Indicates if the registered component is a RenderFunction
96+
* @see RenderFunction for more details on its behavior and usage.
97+
*/
7498
renderFunction: boolean;
7599
// Indicates if the registered component is a Renderer function.
76100
// Renderer function handles DOM rendering or hydration with 3 args: (props, railsContext, domNodeId)

0 commit comments

Comments
 (0)