@@ -128,6 +128,84 @@ class JsRef {
128
128
129
129
/// Automatically passes a [Ref] through a component to one of its children.
130
130
///
131
+ /// __Example 1:__ Forwarding refs to DOM components
132
+ ///
133
+ /// _[Analogous JS Demo] (https://reactjs.org/docs/forwarding-refs.html#forwarding-refs-to-dom-components)_
134
+ ///
135
+ /// ```dart
136
+ /// import 'dart:html';
137
+ /// import 'package:react/react.dart' as react;
138
+ ///
139
+ /// // ---------- Component Definition ----------
140
+ /// final FancyButton = react.forwardRef((props, ref) {
141
+ /// return react.button({'ref': ref, 'className': 'FancyButton'}, 'Click me!');
142
+ /// }, displayName: 'FancyButton');
143
+ ///
144
+ /// // ---------- Component Consumption ----------
145
+ /// void main() {
146
+ /// final ref = createRef<Element>();
147
+ ///
148
+ /// react_dom.render(FancyButton({'ref': ref}));
149
+ ///
150
+ /// // You can now get a ref directly to the DOM button:
151
+ /// final buttonNode = ref.current;
152
+ /// }
153
+ /// ```
154
+ ///
155
+ /// __Example 2:__ Forwarding refs in higher-order components
156
+ ///
157
+ /// _[Analogous JS Demo] (https://reactjs.org/docs/forwarding-refs.html#forwarding-refs-in-higher-order-components)_
158
+ ///
159
+ /// ```dart
160
+ /// import 'dart:html';
161
+ /// import 'package:react/react.dart' as react;
162
+ /// import 'package:react/react_client.dart' show setClientConfiguration, ReactJsComponentFactoryProxy;
163
+ /// import 'package:react/react_dom.dart' as react_dom;
164
+ ///
165
+ /// // ---------- Component Definitions ----------
166
+ ///
167
+ /// final FancyButton = react.forwardRef((props, ref) {
168
+ /// return react.button({'ref': ref, 'className': 'FancyButton'}, 'Click me!');
169
+ /// }, displayName: 'FancyButton');
170
+ ///
171
+ /// class _LogProps extends react.Component2 {
172
+ /// @override
173
+ /// void componentDidUpdate(Map prevProps, _, [__]) {
174
+ /// print('old props: $prevProps');
175
+ /// print('new props: ${this.props}');
176
+ /// }
177
+ ///
178
+ /// @override
179
+ /// render() {
180
+ /// final propsToForward = {...props}..remove('forwardedRef');
181
+ ///
182
+ /// // Assign the custom prop `forwardedRef` as a ref on the component passed in via `props.component`
183
+ /// return props['component']({...propsToForward, 'ref': props['forwardedRef']}, props['children']);
184
+ /// }
185
+ /// }
186
+ /// final _logPropsHoc = react.registerComponent2(() => _LogProps());
187
+ ///
188
+ /// final LogProps = react.forwardRef((props, ref) {
189
+ /// // Note the second param "ref" provided by react.forwardRef.
190
+ /// // We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"
191
+ /// // And it can then be attached to the Component.
192
+ /// return _logPropsHoc({...props, 'forwardedRef': ref});
193
+ /// // Optional: Make the displayName more useful for the React dev tools.
194
+ /// // See: https://reactjs.org/docs/forwarding-refs.html#displaying-a-custom-name-in-devtools
195
+ /// }, displayName: 'LogProps(${_logPropsHoc.type.displayName})');
196
+ ///
197
+ /// // ---------- Component Consumption ----------
198
+ /// void main() {
199
+ /// setClientConfiguration();
200
+ /// final ref = react.createRef<Element>();
201
+ ///
202
+ /// react_dom.render(LogProps({'component': FancyButton, 'ref': ref}),
203
+ /// querySelector('#idOfSomeNodeInTheDom'));
204
+ ///
205
+ /// // You can still get a ref directly to the DOM button:
206
+ /// final buttonNode = ref.current;
207
+ /// }
208
+ /// ```
131
209
/// See: <https://reactjs.org/docs/forwarding-refs.html>.
132
210
ReactJsComponentFactoryProxy forwardRef (Function (Map props, Ref ref) wrapperFunction,
133
211
{String displayName = 'Anonymous' }) {
0 commit comments