@@ -91,7 +91,49 @@ def react_component(component_name, options = {})
91
91
end
92
92
end
93
93
94
+ # Streams a server-side rendered React component using React's `renderToPipeableStream`.
95
+ # Supports React 18 features like Suspense, concurrent rendering, and selective hydration.
96
+ # Enables progressive rendering and improved performance for large components.
97
+ #
98
+ # Note: This function can only be used with React on Rails Pro.
99
+ # The view that uses this function must be rendered using the
100
+ # `stream_view_containing_react_components` method from the React on Rails Pro gem.
101
+ #
102
+ # Example of an async React component that can benefit from streaming:
103
+ #
104
+ # const AsyncComponent = async () => {
105
+ # const data = await fetchData();
106
+ # return <div>{data}</div>;
107
+ # };
108
+ #
109
+ # function App() {
110
+ # return (
111
+ # <Suspense fallback={<div>Loading...</div>}>
112
+ # <AsyncComponent />
113
+ # </Suspense>
114
+ # );
115
+ # }
116
+ #
117
+ # @param [String] component_name Name of your registered component
118
+ # @param [Hash] options Options for rendering
119
+ # @option options [Hash] :props Props to pass to the react component
120
+ # @option options [String] :dom_id DOM ID of the component container
121
+ # @option options [Hash] :html_options Options passed to content_tag
122
+ # @option options [Boolean] :prerender Set to false to disable server-side rendering
123
+ # @option options [Boolean] :trace Set to true to add extra debugging information to the HTML
124
+ # @option options [Boolean] :raise_on_prerender_error Set to true to raise exceptions during server-side rendering
125
+ # Any other options are passed to the content tag, including the id.
94
126
def stream_react_component ( component_name , options = { } )
127
+ unless ReactOnRails ::Utils . react_on_rails_pro?
128
+ raise ReactOnRails ::Error ,
129
+ "You must use React on Rails Pro to use the stream_react_component method."
130
+ end
131
+
132
+ if @rorp_rendering_fibers . nil?
133
+ raise ReactOnRails ::Error ,
134
+ "You must call stream_view_containing_react_components to render the view containing the react component"
135
+ end
136
+
95
137
rendering_fiber = Fiber . new do
96
138
stream = internal_stream_react_component ( component_name , options )
97
139
stream . each_chunk do |chunk |
@@ -100,11 +142,6 @@ def stream_react_component(component_name, options = {})
100
142
Fiber . yield nil
101
143
end
102
144
103
- if @rorp_rendering_fibers . nil?
104
- raise ReactOnRails ::Error ,
105
- "You must call stream_view_containing_react_components to render the view containing the react component"
106
- end
107
-
108
145
@rorp_rendering_fibers << rendering_fiber
109
146
110
147
# return the first chunk of the fiber
0 commit comments