Skip to content

Commit 8740441

Browse files
committed
Rename hooks
1 parent 055d4eb commit 8740441

File tree

9 files changed

+40
-38
lines changed

9 files changed

+40
-38
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ The communication between React app and React Native app will be also simplified
2525
- `.js`, `.ts`, `.jsx`, `.tsx`, `.mjs` and `.cjs` will be packed into one source.
2626
- NOTE: Only the edits in the entry file of web will invoke rebuild because of the limitation of [metro](https://github.com/facebook/metro)'s build process.
2727
- Handle communication between React Native and WebView with React hook style
28-
- With `useBridge` hook, you can subscribe messages from WebView.
29-
- With `useSubscribe` hook, you can subscribe messages from React Native.
28+
- With `useWebViewMessage` hook, you can subscribe messages from WebView.
29+
- With `useNativeMessage` hook, you can subscribe messages from React Native.
3030
- `emit` function sends message.
3131
- Support bundling some assets in web side with [ES6 import syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)
3232
- `.json` is imported as an object, like require in Node.js.
@@ -117,7 +117,7 @@ import React, { useState } from "react";
117117
import {
118118
webViewRender,
119119
emit,
120-
useSubscribe,
120+
useNativeMessage,
121121
} from "react-native-react-bridge/lib/web";
122122
// Importing css is supported
123123
import "./example.css";
@@ -126,8 +126,8 @@ import image from "./foo.png";
126126

127127
const Root = () => {
128128
const [data, setData] = useState("");
129-
// useSubscribe hook receives message from React Native
130-
useSubscribe((message) => {
129+
// useNativeMessage hook receives message from React Native
130+
useNativeMessage((message) => {
131131
if (message.type === "success") {
132132
setData(message.data);
133133
}
@@ -160,13 +160,13 @@ export default webViewRender(<Root />);
160160

161161
import React from "react";
162162
import WebView from "react-native-webview";
163-
import { useBridge } from "react-native-react-bridge";
163+
import { useWebViewMessage } from "react-native-react-bridge";
164164
import webApp from "./WebApp";
165165

166166
const App = () => {
167-
// useBridge hook create props for WebView and handle communication
167+
// useWebViewMessage hook create props for WebView and handle communication
168168
// The argument is callback to receive message from React
169-
const { ref, onMessage, emit } = useBridge((message) => {
169+
const { ref, onMessage, emit } = useWebViewMessage((message) => {
170170
// emit sends message to React
171171
// type: event name
172172
// data: some data which will be serialized by JSON.stringify

src/bridge.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import WebView, {
66
import { EVENT_KEY } from "./common";
77
import { Message } from "./types";
88

9-
export const useBridge = <T>(onSubscribe: (message: Message<T>) => void) => {
9+
export const useWebViewMessage = <T>(
10+
onSubscribe: (message: Message<T>) => void
11+
) => {
1012
const ref = useRef<WebView>(null);
1113
const onMessage: WebViewProps["onMessage"] = useCallback(
1214
(event: WebViewMessageEvent) => {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export type { Message } from "./types";
2-
export { useBridge } from "./bridge";
2+
export { useWebViewMessage } from "./bridge";

src/plugin/__snapshots__/index.spec.js.snap

+12-12
Large diffs are not rendered by default.

src/plugin/__snapshots__/metro.spec.js.snap

+10-10
Large diffs are not rendered by default.

src/web/bridge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const emit = <T>(message: Message<T>) => {
1212
(window as any).ReactNativeWebView.postMessage(JSON.stringify(message));
1313
};
1414

15-
export const buildUseSubscribe = (
15+
export const buildUseNativeMessage = (
1616
useEffect: (cb: () => void, deps: any[]) => void
1717
) => {
1818
return <T>(onSubscribe: (message: Message<T>) => void) => {

src/web/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { webViewRender, emit, useSubscribe } from "./react";
1+
export { webViewRender, emit, useNativeMessage } from "./react";

src/web/preact.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useEffect } from "preact/compat";
22
import { render, ComponentChild } from "preact";
3-
import { buildRender, buildUseSubscribe } from "./bridge";
3+
import { buildRender, buildUseNativeMessage } from "./bridge";
44

55
export const webViewRender = buildRender<ComponentChild>(render);
66

77
export { emit } from "./bridge";
88

9-
export const useSubscribe = buildUseSubscribe(useEffect);
9+
export const useNativeMessage = buildUseNativeMessage(useEffect);

src/web/react.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ReactNode, useEffect } from "react";
22
import { render } from "react-dom";
3-
import { buildRender, buildUseSubscribe } from "./bridge";
3+
import { buildRender, buildUseNativeMessage } from "./bridge";
44

55
export const webViewRender = buildRender<ReactNode>(render);
66

77
export { emit } from "./bridge";
88

9-
export const useSubscribe = buildUseSubscribe(useEffect);
9+
export const useNativeMessage = buildUseNativeMessage(useEffect);

0 commit comments

Comments
 (0)