11import * as ReactDOM from 'react-dom' ;
22import 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' ;
94
105import { getContextAndRailsContext , resetContextAndRailsContext , type Context } from './context' ;
116import createReactOutput from './createReactOutput' ;
@@ -27,9 +22,11 @@ function delegateToRenderer(
2722
2823 if ( isRenderer ) {
2924 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+ ) ;
3330 }
3431
3532 ( component as RenderFunction ) ( props , railsContext , domNodeId ) ;
@@ -39,7 +36,8 @@ DELEGATING TO RENDERER ${name} for dom node with id: ${domNodeId} with props, ra
3936 return false ;
4037}
4138
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' ) || '' ;
4341class ComponentRenderer {
4442 private domNodeId : string ;
4543 private state : 'unmounted' | 'rendering' | 'rendered' ;
@@ -50,22 +48,23 @@ class ComponentRenderer {
5048 const domId = getDomId ( domIdOrElement ) ;
5149 this . domNodeId = domId ;
5250 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 ;
5453 if ( ! el ) return ;
5554
5655 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 [ ] ) : [ ] ;
5857
5958 const { context, railsContext } = getContextAndRailsContext ( ) ;
6059 if ( ! context || ! railsContext ) return ;
6160
6261 // Wait for all store dependencies to be loaded
6362 this . renderPromise = Promise . all (
64- storeDependenciesArray . map ( storeName => context . ReactOnRails . getOrWaitForStore ( storeName ) ) ,
63+ storeDependenciesArray . map ( ( storeName ) => context . ReactOnRails . getOrWaitForStore ( storeName ) ) ,
6564 ) . 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+ } ) ;
6968 }
7069
7170 /**
@@ -76,7 +75,7 @@ class ComponentRenderer {
7675 // This must match lib/react_on_rails/helper.rb
7776 const name = el . getAttribute ( 'data-component-name' ) || '' ;
7877 const { domNodeId } = this ;
79- const props = ( el . textContent !== null ) ? JSON . parse ( el . textContent ) : { } ;
78+ const props = el . textContent !== null ? JSON . parse ( el . textContent ) : { } ;
8079 const trace = el . getAttribute ( 'data-trace' ) === 'true' ;
8180
8281 try {
@@ -109,7 +108,11 @@ class ComponentRenderer {
109108 You returned a server side type of react-router error: ${ JSON . stringify ( reactElementOrRouterResult ) }
110109 You should return a React.Component always for the client side entry point.` ) ;
111110 } else {
112- const rootOrElement = reactHydrateOrRender ( domNode , reactElementOrRouterResult as ReactElement , shouldHydrate ) ;
111+ const rootOrElement = reactHydrateOrRender (
112+ domNode ,
113+ reactElementOrRouterResult as ReactElement ,
114+ shouldHydrate ,
115+ ) ;
113116 this . state = 'rendered' ;
114117 if ( supportsRootApi ) {
115118 this . root = rootOrElement as Root ;
@@ -119,7 +122,7 @@ class ComponentRenderer {
119122 } catch ( e : unknown ) {
120123 const error = e instanceof Error ? e : new Error ( e ?. toString ( ) ?? 'Unknown error' ) ;
121124 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.` ;
123126 throw error ;
124127 }
125128 }
@@ -144,8 +147,11 @@ class ComponentRenderer {
144147 ReactDOM . unmountComponentAtNode ( domNode ) ;
145148 } catch ( e : unknown ) {
146149 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+ ) ;
149155 }
150156 }
151157 }
@@ -170,11 +176,16 @@ class StoreRenderer {
170176 }
171177
172178 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 ) : { } ;
174180 this . hydratePromise = this . hydrate ( context , railsContext , name , props ) ;
175181 }
176182
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+ ) {
178189 const storeGenerator = await context . ReactOnRails . getOrWaitForStoreGenerator ( name ) ;
179190 if ( this . state === 'unmounted' ) {
180191 return ;
@@ -229,10 +240,16 @@ function unmountAllComponents(): void {
229240const storeRenderers = new Map < string , StoreRenderer > ( ) ;
230241
231242export 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 ) || '' ;
233247 let storeRenderer = storeRenderers . get ( storeName ) ;
234248 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 ;
236253 if ( ! storeDataElement ) {
237254 return ;
238255 }
0 commit comments