Skip to content

createWebView ​

The createWebView is used to create a WebView with an interface that enables communication with the web.

Options ​

PropTypeRequiredDefaultDescription
bridgeObjecttrueXRepresents the bridge between React Native and the WebView.
debugbooleanfalsefalseOutputs console.log from the web in React Native.
responseTimeoutnumberfalse2000Timeout duration when executing web methods.
fallback(method: keyof T) => voidfalseXCallback function called when a method from the bridge is not found.
postMessageSchemaObjectfalseXSchemas of the events sent with postMessage: Standard Schemas (Zod, Valibot, ...) or { validate } objects. See Using a PostMessage.

Return value ​

MemberDescription
WebViewThe WebView component connected to the bridge.
postMessage(eventName, payload, { broadcast })Sends an event to the Web, to the last mounted WebView or to all of them with broadcast: true. Validates the payload with postMessageSchema.
linkWebMethodDeprecated. Calls methods registered with registerWebMethod; use postMessage instead.

WebView props ​

The returned WebView accepts every prop of react-native-webview, plus:

PropTypeRequiredDescription
delegateBridgeDelegate<AppBridge>true when the bridge has delegate() methodsImplementations for the methods declared with delegate(). Runs with this WebView's latest props.

Delegated methods ​

delegate() declares a bridge method whose implementation is provided per WebView through the WebView's delegate prop, so it can use hooks and context.

FormPage signatureRuntime validation
delegate()() => Promise<void>none
delegate({ input })(input: InferInput<input>) => Promise<unknown>input
delegate({ output })() => Promise<InferOutput<output>>output
delegate({ input, output })(input: InferInput<input>) => Promise<InferOutput<output>>input and output
delegate<Signature>()Signaturenone

input and output are Standard Schemas (Zod, Valibot, ArkType, ...). The native implementation receives the input schema's output type and returns the output schema's input type. A value that fails a schema makes the call fail with a BridgeValidationError.

tsx
import { bridge, createWebView, delegate } from "@webview-bridge/react-native";
import { z } from "zod";

export const appBridge = bridge({
  goBack: delegate(),
  setTitle: delegate({ input: z.string() }),
});

export const { WebView } = createWebView({ bridge: appBridge });

function Screen() {
  const navigation = useNavigation();
  return (
    <WebView
      delegate={{
        goBack: async () => navigation.goBack(),
        setTitle: async (title) => navigation.setOptions({ title }),
      }}
    />
  );
}

A delegated method is exposed to a page only when its WebView has the handler at mount time. See Using React Hooks in Native Methods.

Bridge middleware ​

bridge(...).use(...) registers instance-scoped middleware for Web-to-Native method calls.

tsx
import {
  bridge,
  type BridgeMiddleware,
} from "@webview-bridge/react-native";

const logger: BridgeMiddleware = async ({ method }, next) => {
  console.log(`Calling ${method}`);
  const result = await next();
  console.log(`Called ${method}`);
  return result;
};

const appBridge = bridge({
  async getMessage() {
    return "Hello, I'm native";
  },
}).use(logger);

Middleware receives { url, method, args } and next(). It may update args, attach typed context fields to the shared request, short-circuit without calling next(), or transform the returned value. use() returns the same bridge store for chaining; call next() at most once.

url is the top-level URL reported by react-native-webview, not a verified caller origin. Middleware applies only to bridge method calls and does not intercept navigation or network requests.