forwardRef2
JSAny
forwardRef2(
Creates a React component that forwards the ref attribute to a child.
Use this when you need to pass a ref through a component to one of its children.
Example:
final FancyButton = forwardRef2((props, ref) {
return button(
props: {'ref': ref, 'className': 'fancy-button'},
text: props['label'] as String,
);
});
// Usage:
final buttonRef = createRef<Element>();
createElement(FancyButton, createProps({
'label': 'Click me!',
'ref': buttonRef.jsRef,
}));
Implementation
JSAny forwardRef2(ForwardRefRenderFunction render, {String? displayName}) {
JSAny jsRender(JSObject jsProps, JSAny? jsRef) {
final dartified = jsProps.dartify();
final props = (dartified is Map<Object?, Object?>)
? dartified.cast<String, Object?>()
: <String, Object?>{};
final ref = (jsRef != null) ? JsRef.fromJs(jsRef as JSObject) : null;
return render(props, ref);
}
final component = _reactForwardRef(jsRender.toJS);
if (displayName != null) {
(component as JSObject)['displayName'] = displayName.toJS;
}
return component;
}