getDartComponent<T extends Component> function

T? getDartComponent<T extends Component>(
  1. dynamic instance
)

Returns the native Dart component associated with a mounted component instance.

Returns null if the instance is not Dart-based (an Element or a JS composite component).

Implementation

// ignore: deprecated_member_use
T? getDartComponent<T extends react.Component>(dynamic /* ReactElement|ReactComponent|Element */ instance) {
  if (instance is Element) {
    return null;
  }

  bool instanceIsMounted() {
    if (isValidElement(instance)) {
      // `print` instead of `ValidationUtil.warn` so that this message shows up
      // in the test output when running `ddev test`.
      print(unindent(
          '''
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
          * WARNING: `getDartComponent`
          *
          * react-dart 4.0 no longer supports retrieving Dart components from
          * `ReactElement` (pre-mounted VDOM instances) in order to prevent memory leaks.
          *
          * This call to `getDartComponent` will return `null`.
          *
          * Start using the mounted JS component instance instead:
          *
          * Example:
          *     // Before
          *     var vdom = Button()('Click me');
          *     react_dom.render(vdom, mountNode);
          *     var dartInstance = getDartComponent(vdom);
          *
          *     // Fixed
          *     var vdom = Button()('Click me');
          *     var jsInstance = react_dom.render(vdom, mountNode);
          *     var dartInstance = getDartComponent(jsInstance);
          *
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
          '''
      ));
    }
    return true;
  }

  assert(instanceIsMounted());

  return (instance as ReactComponent).dartComponent as T?;
}