# Native hooks

A native hook is Swift or Kotlin in your own Expo module that gets the real `WKWebView` or `WebView` behind a `<WebView>`. Its JS side is an *extension*: a [`HookExtension`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/HookExtension/>) with the hook's key, its options and its `onEvent` and `onAsk` handlers. The app passes it in the `extensions` prop.

## When to write one

Reach for a hook when the props and the ref can't do the job:

| To | Use |
| --- | --- |
| Change a setting | The [`ios` and `android` props](<https://rasmuskard.github.io/react-native-minimal-webview/guides/platform-apis/index.md#2-write-a-setting>) |
| Read a property or call a method once | The ref's [`get` and `call`](<https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/index.md#reading-and-calling>) |
| Bring back a react-native-webview prop | A hook from [@react-native-minimal-webview/extensions](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/extensions/>) |
| Handle WebView callbacks, keep native state, or subclass the WebView | A native hook |

A hook can implement any delegate or client callback. It runs natively, with no JS round trip.

## How a hook works

1. Your module registers the hook under a key when the app starts.
2. The app turns the hook on for a WebView by passing its extension in the `extensions` prop; [`HookExtension.options`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/HookExtension/>) says when it is on or off.
3. The core calls the hook's `attach` with the native WebView and a context.
4. The hook adds its delegates or clients to the context, talks to JS through it, and registers its cleanup with `context.onDestroyed`.

## Write a hook

This builds a hook that counts page loads, reports each one to JS, and answers `ref.hook` calls. It is a trimmed `sample.pageCounter` from [sample-extension](<https://github.com/RasmusKard/react-native-minimal-webview/tree/main/example/modules/sample-extension>).

### 1\. Create a module

```sh
npx create-expo-module@latest --local page-counter
```

This creates `modules/page-counter` in your app. A hook in a published package works the same way, with `react-native-minimal-webview` as a peer dependency.

### 2\. Depend on the core

In `ios/PageCounter.podspec`, replace the template's `s.platforms` with the core's iOS 17.0, or importing the core fails to compile, and add the core:

```ruby
s.platforms = { :ios => '17.0' }
s.dependency 'MinimalWebView'
```

In the module's `android/build.gradle`:

```groovy
dependencies {
  implementation project(':react-native-minimal-webview')
}
```

### 3\. Register the hook on iOS

In `ios/PageCounterModule.swift`:

```swift
import ExpoModulesCore
import MinimalWebView
import WebKit

public class PageCounterModule: Module {
  public func definition() -> ModuleDefinition {
    Name("PageCounter")
    OnCreate {
      // The first argument is the WKWebView, which this hook doesn't use
      MinimalWebViewHooks.register("myApp.pageCounter") { _, context in
        let counter = PageCounter(context: context)
        context.navigationDelegates.append(counter)
        context.handle("count") { _, reply in reply(counter.count) }
      }
    }
  }
}

private final class PageCounter: NSObject, WKNavigationDelegate {
  private let context: WebViewContext
  private(set) var count = 0

  init(context: WebViewContext) {
    self.context = context
  }

  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    count += 1
    context.emit("pageLoaded", ["count": count])
  }
}
```

### 4\. Register the hook on Android

In `android/src/main/java/expo/modules/pagecounter/PageCounterModule.kt`:

```kotlin
package expo.modules.pagecounter

import android.webkit.WebView
import android.webkit.WebViewClient
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
import expo.modules.minimalwebview.MinimalWebViewHooks
import expo.modules.minimalwebview.WebViewContext

class PageCounterModule : Module() {
  override fun definition() = ModuleDefinition {
    Name("PageCounter")
    OnCreate {
      // The first argument is the WebView, which this hook doesn't use
      MinimalWebViewHooks.register("myApp.pageCounter") { _, context ->
        val counter = PageCounter(context)
        context.clients += counter
        context.handle("count") { _, reply -> reply(counter.count) }
      }
    }
  }
}

private class PageCounter(private val context: WebViewContext) : WebViewClient() {
  var count = 0
    private set

  override fun onPageFinished(view: WebView, url: String?) {
    count += 1
    context.emit("pageLoaded", mapOf("count" to count))
  }
}
```

### 5\. Describe the hook to JS

Replace the generated `index.ts` with a function that returns the hook's [`HookExtension`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/HookExtension/>). Delete the generated `src/`, `ios/PageCounterView.swift` and `android/.../PageCounterView.kt`: they hold the template's view and module wrappers, which nothing uses.

```ts
import type { HookEvent, HookExtension } from 'react-native-minimal-webview';

type PageLoaded = HookEvent<'pageLoaded', { count: number }>;

export const pageCounter = (onLoaded: (count: number) => void): HookExtension<PageLoaded> => ({
  key: 'myApp.pageCounter',
  onEvent: (event) => {
    if (event.name !== 'pageLoaded') return;
    onLoaded(event.payload.count);
  },
});
```

### 6\. Use it

Pass the extension in the WebView's [`extensions`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#extensions>) prop:

```tsx
import { useRef, useState } from 'react';
import { Button } from 'react-native';
import { WebView, type WebViewRef } from 'react-native-minimal-webview';

import { pageCounter } from '../modules/page-counter';

export function Browser() {
  const ref = useRef<WebViewRef>(null);
  const [count, setCount] = useState(0);
  const logCount = async () => {
    console.log(await ref.current?.hook('myApp.pageCounter', 'count')); // the handler's reply
  };
  return (
    <>
      <Button title={`${count} pages`} onPress={logCount} />
      <WebView
        ref={ref}
        source={{ uri: 'https://example.com' }}
        style={{ flex: 1 }}
        extensions={[pageCounter(setCount)]}
      />
    </>
  );
}
```

Then rebuild the app: `npx expo run:ios`, `npx expo run:android`.

## Talk to JS

Everything crosses as JSON. `WebViewContext` ([Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/webviewcontext/>), [Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/-web-view-context/>)) says which native types values from JS arrive as, what happens to an unanswered `ask`, and what happens when a `handle` handler fails or never replies.

| Native | JS |
| --- | --- |
| `context.emit(name, payload)` | `onEvent` gets an event with that `name` and `payload` |
| `context.ask(name, payload, onAnswer)` | `onAsk` gets the same, or `onEvent` without one; what it returns, awaited if it is a promise, is the answer |
| `context.handle(method, handler)` | [`ref.hook(key, method, args)`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#hook>) resolves with `reply(value)`, or rejects with `handler_error` on `reply.fail(message)` |
| `context.options`, `context.onOptions(handler)` | The extension's `options`: the handler gets them at once, then each change |

## Share the WebView with the core

The core owns the WebView's own delegate and client slots, among what `MinimalWebViewHooks` ([Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/minimalwebviewhooks/>), [Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/-minimal-web-view-hooks/>)) says to leave alone; your hook adds to the context's lists instead.

A callback the core also handles reaches the core first, then each hook. A decision, such as whether a navigation loads or a page gets the camera, goes to the first hook that implements it, and switches off the props that make it, such as `navigationRules`. The rules are on `WebViewContext`'s `navigationDelegates` ([Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/webviewcontext/>)) and `clients` ([Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/-web-view-context/>)).

## Lifecycle

| When | What the hook gets |
| --- | --- |
| The WebView mounts with the hook's key | Its `webViewClass` (iOS) or `webViewFactory` (Android), if the hook has one; then `configure`, if it has one; then `attach` |
| The hook's key turns on after mount | `attach` runs then, unless the hook has `configure` or its own WebView class, or, on Android, is registered with `newWebViewsOnly` |
| The hook's `options` change | Its `onOptions` handlers run, without a reload |
| The hook's key is removed, or its `options` become null or false | `onOptions` with null: the hook is [off](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/HookExtension/>) and stays attached. When it turns on again, `onOptions` runs with the new options. |
| The page ends: a navigation commits, a crash, or unmount | Its `onDocumentEnd` handlers run. Reset per-page state there. |
| The renderer crashes (iOS) | `onDocumentEnd` with `crashed`; the `WKWebView` reloads and nothing attaches again |
| The renderer crashes (Android) | `onDocumentEnd`, then `onDestroyed`, with `CRASHED`; then the factory, `configure` and `attach` run again, on a new context |
| The WebView unmounts | `onDocumentEnd`, then `onDestroyed`, with `closing` (`CLOSING` on Android): clean up there |

`DocumentEnd` ([Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/documentend/>), [Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/-document-end/>)) says when a page ends and what each reason means. `onDestroyed` says what is still alive when it runs.

## Registering and threading

- Register in `OnCreate`, under a key prefixed with your package's name: `register` on `MinimalWebViewHooks` ([Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/minimalwebviewhooks/>), [Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/-minimal-web-view-hooks/>)) has the rules.
- Use the context as `WebViewContext` ([Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/webviewcontext/>), [Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/-web-view-context/>)) says: some members work from any thread, the rest from the main thread.

## Platform differences

| Hook API | iOS | Android |
| --- | --- | --- |
| Callbacks go in | `navigationDelegates`, `uiDelegates` | `clients`, `chromeClients`, `touchListeners`, `scrollListeners` |
| `configure` gets | The `WKWebViewConfiguration`, before the view exists | The new `WebView`, before the core sets it up |
| Your own WebView class | `webViewClass:` | `webViewFactory =` |
| Only on this platform | `refreshControl` | `onDownloadStart` |

## Reference

- Native API: the [Swift](<https://rasmuskard.github.io/react-native-minimal-webview/swift/documentation/minimalwebview/>) and [Kotlin](<https://rasmuskard.github.io/react-native-minimal-webview/kotlin/react-native-minimal-webview/expo.modules.minimalwebview/>) reference, with `MinimalWebViewHooks` and `WebViewContext`.
- JS API: [`extensions`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#extensions>), [`HookExtension`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/HookExtension/>) and [`ref.hook`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#hook>).
- [sample-extension](<https://github.com/RasmusKard/react-native-minimal-webview/tree/main/example/modules/sample-extension>): a hook for each seam the core offers, run by the example app's tests.
- [@react-native-minimal-webview/extensions](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/extensions/>): ready-made hooks. Its README shows how to copy one into your own module.
