@@ -15,6 +15,7 @@ import type {
1515 AuthenticityHeaders ,
1616 Store ,
1717 StoreGenerator ,
18+ ReactOnRailsOptions ,
1819} from './types' ;
1920import reactHydrateOrRender from './reactHydrateOrRender' ;
2021
@@ -40,11 +41,7 @@ const DEFAULT_OPTIONS = {
4041
4142ctx . ReactOnRails = {
4243 options : { } ,
43- /**
44- * Main entry point to using the react-on-rails npm package. This is how Rails will be able to
45- * find you components for rendering.
46- * @param components (key is component name, value is component)
47- */
44+
4845 register ( components : Record < string , ReactComponentOrRenderFunction > ) : void {
4946 ComponentRegistry . register ( components ) ;
5047 } ,
@@ -53,12 +50,6 @@ ctx.ReactOnRails = {
5350 this . registerStoreGenerators ( stores ) ;
5451 } ,
5552
56- /**
57- * Allows registration of store generators to be used by multiple React components on one Rails
58- * view. store generators are functions that take one arg, props, and return a store. Note that
59- * the setStore API is different in that it's the actual store hydrated with props.
60- * @param storeGenerators (keys are store names, values are the store generators)
61- */
6253 registerStoreGenerators ( storeGenerators : Record < string , StoreGenerator > ) : void {
6354 if ( ! storeGenerators ) {
6455 throw new Error (
@@ -70,55 +61,23 @@ ctx.ReactOnRails = {
7061 StoreRegistry . register ( storeGenerators ) ;
7162 } ,
7263
73- /**
74- * Allows retrieval of the store by name. This store will be hydrated by any Rails form props.
75- * Pass optional param throwIfMissing = false if you want to use this call to get back null if the
76- * store with name is not registered.
77- * @param name
78- * @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
79- * there is no store with the given name.
80- * @returns Redux Store, possibly hydrated
81- */
8264 getStore ( name : string , throwIfMissing = true ) : Store | undefined {
8365 return StoreRegistry . getStore ( name , throwIfMissing ) ;
8466 } ,
8567
86- /**
87- * Get a store by name, or wait for it to be registered.
88- * @param name
89- * @returns Promise<Store>
90- */
9168 getOrWaitForStore ( name : string ) : Promise < Store > {
9269 return StoreRegistry . getOrWaitForStore ( name ) ;
9370 } ,
9471
95- /**
96- * Get a store generator by name, or wait for it to be registered.
97- * @param name
98- * @returns Promise<StoreGenerator>
99- */
10072 getOrWaitForStoreGenerator ( name : string ) : Promise < StoreGenerator > {
10173 return StoreRegistry . getOrWaitForStoreGenerator ( name ) ;
10274 } ,
10375
104- /**
105- * Renders or hydrates the React element passed. In case React version is >=18 will use the root API.
106- * @param domNode
107- * @param reactElement
108- * @param hydrate if true will perform hydration, if false will render
109- * @returns {Root|ReactComponent|ReactElement|null }
110- */
11176 reactHydrateOrRender ( domNode : Element , reactElement : ReactElement , hydrate : boolean ) : RenderReturnType {
11277 return reactHydrateOrRender ( domNode , reactElement , hydrate ) ;
11378 } ,
11479
115- /**
116- * Set options for ReactOnRails, typically before you call ReactOnRails.register
117- * Available Options:
118- * `traceTurbolinks: true|false Gives you debugging messages on Turbolinks events
119- * `turbo: true|false Turbo (the follower of Turbolinks) events will be registered, if set to true.
120- */
121- setOptions ( newOptions : { traceTurbolinks ?: boolean ; turbo ?: boolean } ) : void {
80+ setOptions ( newOptions : Partial < ReactOnRailsOptions > ) : void {
12281 if ( typeof newOptions . traceTurbolinks !== 'undefined' ) {
12382 this . options . traceTurbolinks = newOptions . traceTurbolinks ;
12483
@@ -138,12 +97,6 @@ ctx.ReactOnRails = {
13897 }
13998 } ,
14099
141- /**
142- * Allow directly calling the page loaded script in case the default events that trigger React
143- * rendering are not sufficient, such as when loading JavaScript asynchronously with TurboLinks:
144- * More details can be found here:
145- * https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/turbolinks.md
146- */
147100 reactOnRailsPageLoaded ( ) {
148101 return ClientStartup . reactOnRailsPageLoaded ( ) ;
149102 } ,
@@ -156,21 +109,10 @@ ctx.ReactOnRails = {
156109 return hydrateStore ( storeName ) ;
157110 } ,
158111
159- /**
160- * Returns CSRF authenticity token inserted by Rails csrf_meta_tags
161- * @returns String or null
162- */
163-
164112 authenticityToken ( ) : string | null {
165113 return Authenticity . authenticityToken ( ) ;
166114 } ,
167115
168- /**
169- * Returns header with csrf authenticity token and XMLHttpRequest
170- * @param otherHeaders Other headers
171- * @returns {* } header
172- */
173-
174116 authenticityHeaders ( otherHeaders : Record < string , string > = { } ) : AuthenticityHeaders {
175117 return Authenticity . authenticityHeaders ( otherHeaders ) ;
176118 } ,
@@ -179,66 +121,22 @@ ctx.ReactOnRails = {
179121 // INTERNALLY USED APIs
180122 // /////////////////////////////////////////////////////////////////////////////
181123
182- /**
183- * Retrieve an option by key.
184- * @param key
185- * @returns option value
186- */
187- option ( key : string ) : string | number | boolean | undefined {
124+ option < K extends keyof ReactOnRailsOptions > ( key : K ) : ReactOnRailsOptions [ K ] | undefined {
188125 return this . options [ key ] ;
189126 } ,
190127
191- /**
192- * Allows retrieval of the store generator by name. This is used internally by ReactOnRails after
193- * a Rails form loads to prepare stores.
194- * @param name
195- * @returns Redux Store generator function
196- */
197128 getStoreGenerator ( name : string ) : StoreGenerator {
198129 return StoreRegistry . getStoreGenerator ( name ) ;
199130 } ,
200131
201- /**
202- * Allows saving the store populated by Rails form props. Used internally by ReactOnRails.
203- * @returns Redux Store, possibly hydrated
204- */
205132 setStore ( name : string , store : Store ) : void {
206133 StoreRegistry . setStore ( name , store ) ;
207134 } ,
208135
209- /**
210- * Clears hydratedStores to avoid accidental usage of wrong store hydrated in previous/parallel
211- * request.
212- */
213136 clearHydratedStores ( ) : void {
214137 StoreRegistry . clearHydratedStores ( ) ;
215138 } ,
216139
217- /**
218- * @example
219- * ReactOnRails.render("HelloWorldApp", {name: "Stranger"}, 'app');
220- *
221- * Does this:
222- * ```js
223- * ReactDOM.render(React.createElement(HelloWorldApp, {name: "Stranger"}),
224- * document.getElementById('app'))
225- * ```
226- * under React 16/17 and
227- * ```js
228- * const root = ReactDOMClient.createRoot(document.getElementById('app'))
229- * root.render(React.createElement(HelloWorldApp, {name: "Stranger"}))
230- * return root
231- * ```
232- * under React 18+.
233- *
234- * @param name Name of your registered component
235- * @param props Props to pass to your component
236- * @param domNodeId
237- * @param hydrate Pass truthy to update server rendered html. Default is falsy
238- * @returns {Root|ReactComponent|ReactElement } Under React 18+: the created React root
239- * (see "What is a root?" in https://github.com/reactwg/react-18/discussions/5).
240- * Under React 16/17: Reference to your component's backing instance or `null` for stateless components.
241- */
242140 render ( name : string , props : Record < string , string > , domNodeId : string , hydrate : boolean ) : RenderReturnType {
243141 const componentObj = ComponentRegistry . get ( name ) ;
244142 const reactElement = createReactOutput ( { componentObj, props, domNodeId } ) ;
@@ -250,88 +148,48 @@ ctx.ReactOnRails = {
250148 ) ;
251149 } ,
252150
253- /**
254- * Get the component that you registered
255- * @param name
256- * @returns {name, component, renderFunction, isRenderer }
257- */
258151 getComponent ( name : string ) : RegisteredComponent {
259152 return ComponentRegistry . get ( name ) ;
260153 } ,
261154
262- /**
263- * Get the component that you registered, or wait for it to be registered
264- * @param name
265- * @returns {name, component, renderFunction, isRenderer }
266- */
267155 getOrWaitForComponent ( name : string ) : Promise < RegisteredComponent > {
268156 return ComponentRegistry . getOrWaitForComponent ( name ) ;
269157 } ,
270158
271- /**
272- * Used by server rendering by Rails
273- * @param options
274- */
275159 serverRenderReactComponent ( ) : null | string | Promise < RenderResult > {
276160 throw new Error (
277161 'serverRenderReactComponent is not available in "react-on-rails/client". Import "react-on-rails" server-side.' ,
278162 ) ;
279163 } ,
280164
281- /**
282- * Used by server rendering by Rails
283- * @param options
284- */
285165 streamServerRenderedReactComponent ( ) {
286166 throw new Error (
287167 'streamServerRenderedReactComponent is only supported when using a bundle built for Node.js environments' ,
288168 ) ;
289169 } ,
290170
291- /**
292- * Generates RSC payload, used by Rails
293- */
294171 serverRenderRSCReactComponent ( ) {
295172 throw new Error ( 'serverRenderRSCReactComponent is supported in RSC bundle only.' ) ;
296173 } ,
297174
298- /**
299- * Used by Rails to catch errors in rendering
300- * @param options
301- */
302175 handleError ( ) : string | undefined {
303176 throw new Error (
304177 'handleError is not available in "react-on-rails/client". Import "react-on-rails" server-side.' ,
305178 ) ;
306179 } ,
307180
308- /**
309- * Used by Rails server rendering to replay console messages.
310- */
311181 buildConsoleReplay ( ) : string {
312182 return buildConsoleReplay ( ) ;
313183 } ,
314184
315- /**
316- * Get an Object containing all registered components. Useful for debugging.
317- * @returns {* }
318- */
319185 registeredComponents ( ) : Map < string , RegisteredComponent > {
320186 return ComponentRegistry . components ( ) ;
321187 } ,
322188
323- /**
324- * Get an Object containing all registered store generators. Useful for debugging.
325- * @returns {* }
326- */
327189 storeGenerators ( ) : Map < string , StoreGenerator > {
328190 return StoreRegistry . storeGenerators ( ) ;
329191 } ,
330192
331- /**
332- * Get an Object containing all hydrated stores. Useful for debugging.
333- * @returns {* }
334- */
335193 stores ( ) : Map < string , Store > {
336194 return StoreRegistry . stores ( ) ;
337195 } ,
0 commit comments