Command palette

@yasinsensoy/native

React Native · APIs and utilities

Native modules

The native module.

12 exports

Import

import {
  NativeModuleError,
  requireOptionalNativeModule,
  requireNativeModule,
  isNativeModuleAvailable,
  nativeModuleNames,
  requireNativeView,
} from "@yasinsensoy/native";

The native module. Throws a clear error if the module is not in the build; for optional use see requireOptionalNativeModule.

The returned object is lazy: the module is resolved on first property access. So writing export const X = requireNativeModule(...) at the top of a module file is safe (importing it does not throw on web or in tests).

requireNativeModule(name: string): NativeModule<T, Events>

An error coming from the native side.

The native module; null if it is not in this build. The module is resolved once on first call and cached.

requireOptionalNativeModule(name: string): NativeModule<T, Events> | null

Whether the module is in this build.

isNativeModuleAvailable(name: string): boolean

Names of all native modules in this build.

nativeModuleNames(): string[]

Turns a module's native view into a React component. Props (non-function) go to the native view's setProps method as JSON; a native emit("ready", …) event calls the onReady prop.

requireNativeView(module: string, view?: string): {
    (props: P & NativeViewProps): JSX.Element

Examples

interface PreviewProps { facing: "front" | "back"; onReady?: () => void }
const Preview = requireNativeView<PreviewProps>("Camera", "Preview");
<Preview facing="back" style={{ flex: 1 }} ref={ref} />

An event subscription.

NameTypeDescription
remove(): voidRemoves the listener.

Event name → event data mapping.

type ModuleEvents = Record<string, unknown>

Members shared by every module.

NameTypeDescription
moduleNamestringJS name of the module.

A typed native module: the interface you defined + the shared members.

type NativeModule = T & NativeModuleBase<Events>

Extends: T

NameTypeDescription
moduleNamestringJS name of the module.

Handle of the view: this id is passed to module functions.

NameTypeDescription
instanceIdstringId of the view instance (Camera.takePicture(ref.current.instanceId)).

Props shared by every module view.

NameTypeDescription
style?StyleProp<ViewStyle>View style (do not forget to give a size; the content fills the whole area).
testID?stringTest id.
onLayout?(event: LayoutChangeEvent) => voidWhen the layout changes.
ref?Ref<NativeViewHandle>Handle (instanceId).