(moduleName, viewName);
nativeComponentsCache.set(cacheKey, nativeComponent);
return nativeComponent;
}
return cachedNativeComponent;
}
/**
* A drop-in replacement for `requireNativeComponent`.
*/
export function requireNativeViewManager(
moduleName: string,
viewName?: string
): ComponentType
{
const ReactNativeComponent = requireCachedNativeComponent(moduleName, viewName);
class NativeComponent extends PureComponent
{
static displayName = viewName ? viewName : moduleName;
nativeRef = createRef();
// This will be accessed from native when the prototype functions are called,
// in order to find the associated native view.
nativeTag: number | null = null;
componentDidMount(): void {
this.nativeTag = findNodeHandle(this.nativeRef.current);
}
render() {
return ;
}
}
try {
const nativeModule = requireNativeModule(moduleName);
const nativeViewPrototype =
nativeModule.ViewPrototypes[viewName ? `${moduleName}_${viewName}` : moduleName];
if (nativeViewPrototype) {
// Assign native view functions to the component prototype, so they can be accessed from the ref.
Object.assign(NativeComponent.prototype, nativeViewPrototype);
}
} catch {
// `requireNativeModule` may throw an error when the native module cannot be found.
// In some tests we don't mock the entire modules, but we do want to mock native views. For now,
// until we still have to support the legacy modules proxy and don't have better ways to mock,
// let's just gracefully skip assigning the prototype functions.
// See: https://github.com/expo/expo/blob/main/packages/expo-modules-core/src/__tests__/NativeViewManagerAdapter-test.native.tsx
}
return NativeComponent;
}