jsifyRefProp function

dynamic jsifyRefProp(
  1. dynamic value
)

Returns value converted to its JS ref representation for storage in a props map, or null of the value is null.

For use in JS component prop getters where the component expects a JS ref, but accepting Dart refs is more convenient to the consumer reading/writing the props.

Should be used alongside unjsifyRefProp.

Normally ref props get converted in ReactJsComponentFactoryProxy.build and jsifyMapProp, but that same conversion for props not under the default 'ref' key doesn't occur automatically, which is where this function comes in.

Implementation

dynamic jsifyRefProp(dynamic value) {
  // Case 1: null
  if (value == null) return null;

  // Case 2a: Dart callback refs
  // Case 2b: JS callback refs
  // allowInterop is technically redundant, since that will get done in ReactJsComponentFactoryProxy.build
  // (or jsifyMapProp for props that accept props maps, like ButtonProps.TouchRippleProps),
  // but we'll do it here anyways since we know it'll be needed.
  if (value is Function) return allowInterop(value);

  // Case 2: Dart ref objects
  if (value is Ref) {
    // Store the original Dart ref so we can retrieve it later in unjsifyRefProp.
    // See _dartRefForJsRef comment for more info.
    _dartRefForJsRef.set(value.jsRef, value);
    return value.jsRef;
  }

  // Case 3: JS ref objects
  return value;
}