npmComponentSafe

Result<NpmComponentElement, String> npmComponentSafe(

  1. String packageName,
  2. String componentPath, {
  3. Map<String, dynamic>? props,
  4. List<ReactElement>? children,
  5. JSAny? child,
})

Safe version of npmComponent that returns a Result instead of throwing.

Implementation

Result<NpmComponentElement, String> npmComponentSafe(
  String packageName,
  String componentPath, {
  Map<String, dynamic>? props,
  List<ReactElement>? children,
  JSAny? child,
}) {
  final moduleResult = loadNpmModule(packageName);
  if (moduleResult case Error(:final error)) {
    return Error(error);
  }
  final module = (moduleResult as Success<JSObject, String>).value;

final componentResult = getComponentFromModule(module, componentPath); if (componentResult case Error(:final error)) { return Error(error); } final component = (componentResult as Success<JSAny, String>).value;

try { final jsProps = (props != null) ? createProps(props) : null;

final element = (children != null && children.isNotEmpty) ? createElementWithChildren(component, jsProps, children) : (child != null) ? createElement(component, jsProps, child) : createElement(component, jsProps);

return Success(NpmComponentElement.fromJS(element)); } on Object catch (e) { return Error('Failed to create element: $e'); } }