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,12 @@ function delegateToRenderer(
2722
2823 if ( isRenderer ) {
2924 if ( trace ) {
30- console . log ( `\
25+ console . log (
26+ `\
3127DELEGATING TO RENDERER ${ name } for dom node with id: ${ domNodeId } with props, railsContext:` ,
32- props , railsContext ) ;
28+ props ,
29+ railsContext ,
30+ ) ;
3331 }
3432
3533 ( component as RenderFunction ) ( props , railsContext , domNodeId ) ;
@@ -39,7 +37,8 @@ DELEGATING TO RENDERER ${name} for dom node with id: ${domNodeId} with props, ra
3937 return false ;
4038}
4139
42- const getDomId = ( domIdOrElement : string | Element ) : string => typeof domIdOrElement === 'string' ? domIdOrElement : domIdOrElement . getAttribute ( 'data-dom-id' ) || '' ;
40+ const getDomId = ( domIdOrElement : string | Element ) : string =>
41+ typeof domIdOrElement === 'string' ? domIdOrElement : domIdOrElement . getAttribute ( 'data-dom-id' ) || '' ;
4342class ComponentRenderer {
4443 private domNodeId : string ;
4544 private state : 'unmounted' | 'rendering' | 'rendered' ;
@@ -50,22 +49,23 @@ class ComponentRenderer {
5049 const domId = getDomId ( domIdOrElement ) ;
5150 this . domNodeId = domId ;
5251 this . state = 'rendering' ;
53- const el = typeof domIdOrElement === 'string' ? document . querySelector ( `[data-dom-id=${ domId } ]` ) : domIdOrElement ;
52+ const el =
53+ typeof domIdOrElement === 'string' ? document . querySelector ( `[data-dom-id=${ domId } ]` ) : domIdOrElement ;
5454 if ( ! el ) return ;
5555
5656 const storeDependencies = el . getAttribute ( 'data-store-dependencies' ) ;
57- const storeDependenciesArray = storeDependencies ? JSON . parse ( storeDependencies ) as string [ ] : [ ] ;
57+ const storeDependenciesArray = storeDependencies ? ( JSON . parse ( storeDependencies ) as string [ ] ) : [ ] ;
5858
5959 const { context, railsContext } = getContextAndRailsContext ( ) ;
6060 if ( ! context || ! railsContext ) return ;
6161
6262 // Wait for all store dependencies to be loaded
6363 this . renderPromise = Promise . all (
64- storeDependenciesArray . map ( storeName => context . ReactOnRails . getOrWaitForStore ( storeName ) ) ,
64+ storeDependenciesArray . map ( ( storeName ) => context . ReactOnRails . getOrWaitForStore ( storeName ) ) ,
6565 ) . then ( ( ) => {
66- if ( this . state === 'unmounted' ) return Promise . resolve ( ) ;
67- return this . render ( el , context , railsContext ) ;
68- } ) ;
66+ if ( this . state === 'unmounted' ) return Promise . resolve ( ) ;
67+ return this . render ( el , context , railsContext ) ;
68+ } ) ;
6969 }
7070
7171 /**
@@ -76,7 +76,7 @@ class ComponentRenderer {
7676 // This must match lib/react_on_rails/helper.rb
7777 const name = el . getAttribute ( 'data-component-name' ) || '' ;
7878 const { domNodeId } = this ;
79- const props = ( el . textContent !== null ) ? JSON . parse ( el . textContent ) : { } ;
79+ const props = el . textContent !== null ? JSON . parse ( el . textContent ) : { } ;
8080 const trace = el . getAttribute ( 'data-trace' ) === 'true' ;
8181
8282 try {
@@ -109,7 +109,11 @@ class ComponentRenderer {
109109 You returned a server side type of react-router error: ${ JSON . stringify ( reactElementOrRouterResult ) }
110110 You should return a React.Component always for the client side entry point.` ) ;
111111 } else {
112- const rootOrElement = reactHydrateOrRender ( domNode , reactElementOrRouterResult as ReactElement , shouldHydrate ) ;
112+ const rootOrElement = reactHydrateOrRender (
113+ domNode ,
114+ reactElementOrRouterResult as ReactElement ,
115+ shouldHydrate ,
116+ ) ;
113117 this . state = 'rendered' ;
114118 if ( supportsRootApi ) {
115119 this . root = rootOrElement as Root ;
@@ -119,7 +123,7 @@ class ComponentRenderer {
119123 } catch ( e : unknown ) {
120124 const error = e instanceof Error ? e : new Error ( e ?. toString ( ) ?? 'Unknown error' ) ;
121125 console . error ( error . message ) ;
122- error . message = `ReactOnRails encountered an error while rendering component: ${ name } . See above error message.`
126+ error . message = `ReactOnRails encountered an error while rendering component: ${ name } . See above error message.` ;
123127 throw error ;
124128 }
125129 }
@@ -144,8 +148,11 @@ class ComponentRenderer {
144148 ReactDOM . unmountComponentAtNode ( domNode ) ;
145149 } catch ( e : unknown ) {
146150 const error = e instanceof Error ? e : new Error ( 'Unknown error' ) ;
147- console . info ( `Caught error calling unmountComponentAtNode: ${ error . message } for domNode` ,
148- domNode , error ) ;
151+ console . info (
152+ `Caught error calling unmountComponentAtNode: ${ error . message } for domNode` ,
153+ domNode ,
154+ error ,
155+ ) ;
149156 }
150157 }
151158 }
@@ -170,11 +177,16 @@ class StoreRenderer {
170177 }
171178
172179 const name = storeDataElement . getAttribute ( REACT_ON_RAILS_STORE_ATTRIBUTE ) || '' ;
173- const props = ( storeDataElement . textContent !== null ) ? JSON . parse ( storeDataElement . textContent ) : { } ;
180+ const props = storeDataElement . textContent !== null ? JSON . parse ( storeDataElement . textContent ) : { } ;
174181 this . hydratePromise = this . hydrate ( context , railsContext , name , props ) ;
175182 }
176183
177- private async hydrate ( context : Context , railsContext : RailsContext , name : string , props : Record < string , string > ) {
184+ private async hydrate (
185+ context : Context ,
186+ railsContext : RailsContext ,
187+ name : string ,
188+ props : Record < string , string > ,
189+ ) {
178190 const storeGenerator = await context . ReactOnRails . getOrWaitForStoreGenerator ( name ) ;
179191 if ( this . state === 'unmounted' ) {
180192 return ;
@@ -229,10 +241,16 @@ function unmountAllComponents(): void {
229241const storeRenderers = new Map < string , StoreRenderer > ( ) ;
230242
231243export async function hydrateStore ( storeNameOrElement : string | Element ) {
232- const storeName = typeof storeNameOrElement === 'string' ? storeNameOrElement : storeNameOrElement . getAttribute ( REACT_ON_RAILS_STORE_ATTRIBUTE ) || '' ;
244+ const storeName =
245+ typeof storeNameOrElement === 'string'
246+ ? storeNameOrElement
247+ : storeNameOrElement . getAttribute ( REACT_ON_RAILS_STORE_ATTRIBUTE ) || '' ;
233248 let storeRenderer = storeRenderers . get ( storeName ) ;
234249 if ( ! storeRenderer ) {
235- const storeDataElement = typeof storeNameOrElement === 'string' ? document . querySelector ( `[${ REACT_ON_RAILS_STORE_ATTRIBUTE } ="${ storeNameOrElement } "]` ) : storeNameOrElement ;
250+ const storeDataElement =
251+ typeof storeNameOrElement === 'string'
252+ ? document . querySelector ( `[${ REACT_ON_RAILS_STORE_ATTRIBUTE } ="${ storeNameOrElement } "]` )
253+ : storeNameOrElement ;
236254 if ( ! storeDataElement ) {
237255 return ;
238256 }
0 commit comments