1
1
import * as ReactDOM from 'react-dom' ;
2
2
import type { ReactElement } from 'react' ;
3
- import type {
4
- RailsContext ,
5
- RegisteredComponent ,
6
- RenderFunction ,
7
- Root ,
8
- } from './types' ;
3
+ import type { RailsContext , RegisteredComponent , RenderFunction , Root } from './types' ;
9
4
10
5
import { getContextAndRailsContext , resetContextAndRailsContext , type Context } from './context' ;
11
6
import createReactOutput from './createReactOutput' ;
@@ -27,9 +22,11 @@ function delegateToRenderer(
27
22
28
23
if ( isRenderer ) {
29
24
if ( trace ) {
30
- console . log ( `\
31
- DELEGATING TO RENDERER ${ name } for dom node with id: ${ domNodeId } with props, railsContext:` ,
32
- props , railsContext ) ;
25
+ console . log (
26
+ `DELEGATING TO RENDERER ${ name } for dom node with id: ${ domNodeId } with props, railsContext:` ,
27
+ props ,
28
+ railsContext ,
29
+ ) ;
33
30
}
34
31
35
32
( component as RenderFunction ) ( props , railsContext , domNodeId ) ;
@@ -39,7 +36,8 @@ DELEGATING TO RENDERER ${name} for dom node with id: ${domNodeId} with props, ra
39
36
return false ;
40
37
}
41
38
42
- const getDomId = ( domIdOrElement : string | Element ) : string => typeof domIdOrElement === 'string' ? domIdOrElement : domIdOrElement . getAttribute ( 'data-dom-id' ) || '' ;
39
+ const getDomId = ( domIdOrElement : string | Element ) : string =>
40
+ typeof domIdOrElement === 'string' ? domIdOrElement : domIdOrElement . getAttribute ( 'data-dom-id' ) || '' ;
43
41
class ComponentRenderer {
44
42
private domNodeId : string ;
45
43
private state : 'unmounted' | 'rendering' | 'rendered' ;
@@ -50,22 +48,23 @@ class ComponentRenderer {
50
48
const domId = getDomId ( domIdOrElement ) ;
51
49
this . domNodeId = domId ;
52
50
this . state = 'rendering' ;
53
- const el = typeof domIdOrElement === 'string' ? document . querySelector ( `[data-dom-id=${ domId } ]` ) : domIdOrElement ;
51
+ const el =
52
+ typeof domIdOrElement === 'string' ? document . querySelector ( `[data-dom-id=${ domId } ]` ) : domIdOrElement ;
54
53
if ( ! el ) return ;
55
54
56
55
const storeDependencies = el . getAttribute ( 'data-store-dependencies' ) ;
57
- const storeDependenciesArray = storeDependencies ? JSON . parse ( storeDependencies ) as string [ ] : [ ] ;
56
+ const storeDependenciesArray = storeDependencies ? ( JSON . parse ( storeDependencies ) as string [ ] ) : [ ] ;
58
57
59
58
const { context, railsContext } = getContextAndRailsContext ( ) ;
60
59
if ( ! context || ! railsContext ) return ;
61
60
62
61
// Wait for all store dependencies to be loaded
63
62
this . renderPromise = Promise . all (
64
- storeDependenciesArray . map ( storeName => context . ReactOnRails . getOrWaitForStore ( storeName ) ) ,
63
+ storeDependenciesArray . map ( ( storeName ) => context . ReactOnRails . getOrWaitForStore ( storeName ) ) ,
65
64
) . then ( ( ) => {
66
- if ( this . state === 'unmounted' ) return Promise . resolve ( ) ;
67
- return this . render ( el , context , railsContext ) ;
68
- } ) ;
65
+ if ( this . state === 'unmounted' ) return Promise . resolve ( ) ;
66
+ return this . render ( el , context , railsContext ) ;
67
+ } ) ;
69
68
}
70
69
71
70
/**
@@ -76,7 +75,7 @@ class ComponentRenderer {
76
75
// This must match lib/react_on_rails/helper.rb
77
76
const name = el . getAttribute ( 'data-component-name' ) || '' ;
78
77
const { domNodeId } = this ;
79
- const props = ( el . textContent !== null ) ? JSON . parse ( el . textContent ) : { } ;
78
+ const props = el . textContent !== null ? JSON . parse ( el . textContent ) : { } ;
80
79
const trace = el . getAttribute ( 'data-trace' ) === 'true' ;
81
80
82
81
try {
@@ -109,7 +108,11 @@ class ComponentRenderer {
109
108
You returned a server side type of react-router error: ${ JSON . stringify ( reactElementOrRouterResult ) }
110
109
You should return a React.Component always for the client side entry point.` ) ;
111
110
} else {
112
- const rootOrElement = reactHydrateOrRender ( domNode , reactElementOrRouterResult as ReactElement , shouldHydrate ) ;
111
+ const rootOrElement = reactHydrateOrRender (
112
+ domNode ,
113
+ reactElementOrRouterResult as ReactElement ,
114
+ shouldHydrate ,
115
+ ) ;
113
116
this . state = 'rendered' ;
114
117
if ( supportsRootApi ) {
115
118
this . root = rootOrElement as Root ;
@@ -119,7 +122,7 @@ class ComponentRenderer {
119
122
} catch ( e : unknown ) {
120
123
const error = e instanceof Error ? e : new Error ( e ?. toString ( ) ?? 'Unknown error' ) ;
121
124
console . error ( error . message ) ;
122
- error . message = `ReactOnRails encountered an error while rendering component: ${ name } . See above error message.`
125
+ error . message = `ReactOnRails encountered an error while rendering component: ${ name } . See above error message.` ;
123
126
throw error ;
124
127
}
125
128
}
@@ -144,8 +147,11 @@ class ComponentRenderer {
144
147
ReactDOM . unmountComponentAtNode ( domNode ) ;
145
148
} catch ( e : unknown ) {
146
149
const error = e instanceof Error ? e : new Error ( 'Unknown error' ) ;
147
- console . info ( `Caught error calling unmountComponentAtNode: ${ error . message } for domNode` ,
148
- domNode , error ) ;
150
+ console . info (
151
+ `Caught error calling unmountComponentAtNode: ${ error . message } for domNode` ,
152
+ domNode ,
153
+ error ,
154
+ ) ;
149
155
}
150
156
}
151
157
}
@@ -170,11 +176,16 @@ class StoreRenderer {
170
176
}
171
177
172
178
const name = storeDataElement . getAttribute ( REACT_ON_RAILS_STORE_ATTRIBUTE ) || '' ;
173
- const props = ( storeDataElement . textContent !== null ) ? JSON . parse ( storeDataElement . textContent ) : { } ;
179
+ const props = storeDataElement . textContent !== null ? JSON . parse ( storeDataElement . textContent ) : { } ;
174
180
this . hydratePromise = this . hydrate ( context , railsContext , name , props ) ;
175
181
}
176
182
177
- private async hydrate ( context : Context , railsContext : RailsContext , name : string , props : Record < string , string > ) {
183
+ private async hydrate (
184
+ context : Context ,
185
+ railsContext : RailsContext ,
186
+ name : string ,
187
+ props : Record < string , string > ,
188
+ ) {
178
189
const storeGenerator = await context . ReactOnRails . getOrWaitForStoreGenerator ( name ) ;
179
190
if ( this . state === 'unmounted' ) {
180
191
return ;
@@ -229,10 +240,16 @@ function unmountAllComponents(): void {
229
240
const storeRenderers = new Map < string , StoreRenderer > ( ) ;
230
241
231
242
export async function hydrateStore ( storeNameOrElement : string | Element ) {
232
- const storeName = typeof storeNameOrElement === 'string' ? storeNameOrElement : storeNameOrElement . getAttribute ( REACT_ON_RAILS_STORE_ATTRIBUTE ) || '' ;
243
+ const storeName =
244
+ typeof storeNameOrElement === 'string'
245
+ ? storeNameOrElement
246
+ : storeNameOrElement . getAttribute ( REACT_ON_RAILS_STORE_ATTRIBUTE ) || '' ;
233
247
let storeRenderer = storeRenderers . get ( storeName ) ;
234
248
if ( ! storeRenderer ) {
235
- const storeDataElement = typeof storeNameOrElement === 'string' ? document . querySelector ( `[${ REACT_ON_RAILS_STORE_ATTRIBUTE } ="${ storeNameOrElement } "]` ) : storeNameOrElement ;
249
+ const storeDataElement =
250
+ typeof storeNameOrElement === 'string'
251
+ ? document . querySelector ( `[${ REACT_ON_RAILS_STORE_ATTRIBUTE } ="${ storeNameOrElement } "]` )
252
+ : storeNameOrElement ;
236
253
if ( ! storeDataElement ) {
237
254
return ;
238
255
}
0 commit comments