Skip to content

Commit 9366b10

Browse files
committed
Improve emit API
1 parent 43cf30c commit 9366b10

22 files changed

+54
-46
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ module.exports = {
164164
import React, { useState } from "react";
165165
import {
166166
webViewRender,
167-
emit,
167+
emitToNative,
168168
useNativeMessage,
169169
} from "react-native-react-bridge/lib/web";
170170
// Importing css is supported
@@ -189,7 +189,7 @@ const Root = () => {
189189
// emit sends message to React Native
190190
// type: event name
191191
// data: some data which will be serialized by JSON.stringify
192-
emit({ type: "hello", data: 123 });
192+
emitToNative({ type: "hello", data: 123 });
193193
}}
194194
/>
195195
</div>
@@ -208,7 +208,7 @@ export default webViewRender(<Root />);
208208

209209
import React from "react";
210210
import WebView from "react-native-webview";
211-
import { emit, useWebViewMessage } from "react-native-react-bridge";
211+
import { emitToWebView, useWebViewMessage } from "react-native-react-bridge";
212212
import webApp from "./WebApp";
213213

214214
const App = () => {
@@ -220,7 +220,7 @@ const App = () => {
220220
// type: event name
221221
// data: some data which will be serialized by JSON.stringify
222222
if (message.type === "hello" && message.data === 123) {
223-
emit(ref, { type: "success", data: "succeeded!" });
223+
emitToWebView(ref, { type: "success", data: "succeeded!" });
224224
}
225225
});
226226

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-commonjs-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-react-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-tsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-with-html-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-with-images-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-with-json-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-with-md-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-default-with-txt-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-named-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-app-export-non-jsx-3-chromium.txt

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

e2e/index.spec.tsx-snapshots/smoke-webview-issue45-jsx-3-chromium.txt

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

examples/DemoApp/App.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
TextInput,
1717
} from 'react-native';
1818
import WebView from 'react-native-webview';
19-
import {emit, useWebViewMessage} from 'react-native-react-bridge';
19+
import {emitToWebView, useWebViewMessage} from 'react-native-react-bridge';
2020
import webApp from './WebApp';
2121

2222
const App = () => {
@@ -45,7 +45,7 @@ const App = () => {
4545
value={data}
4646
/>
4747
<Pressable
48-
onPress={() => emit(ref, {type: 'hello', data: data})}
48+
onPress={() => emitToWebView(ref, {type: 'hello', data: data})}
4949
style={styles.button}>
5050
<Text>send to Web</Text>
5151
</Pressable>

examples/DemoApp/WebApp/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useState} from 'react';
22
import {
33
webViewRender,
4-
emit,
4+
emitToNative,
55
useNativeMessage,
66
} from 'react-native-react-bridge/lib/web';
77
import './example.css';
@@ -29,7 +29,7 @@ const Root = () => {
2929
</div>
3030
<textarea value={data} onChange={e => setData(e.target.value)} />
3131
<div>
32-
<Button onClick={() => emit({type: 'hi', data: data})}>
32+
<Button onClick={() => emitToNative({type: 'hi', data: data})}>
3333
send to React Native
3434
</Button>
3535
</div>

examples/DemoAppExpo/App.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
TextInput,
1717
} from "react-native";
1818
import WebView from "react-native-webview";
19-
import { emit, useWebViewMessage } from "react-native-react-bridge";
19+
import { emitToWebView, useWebViewMessage } from "react-native-react-bridge";
2020
import webApp from "./WebApp";
2121

2222
const App = () => {
@@ -45,7 +45,7 @@ const App = () => {
4545
value={data}
4646
/>
4747
<Pressable
48-
onPress={() => emit(ref, { type: "hello", data: data })}
48+
onPress={() => emitToWebView(ref, { type: "hello", data: data })}
4949
style={styles.button}
5050
>
5151
<Text>send to Web</Text>

examples/DemoAppExpo/WebApp/index.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import React, {useState} from 'react';
1+
import React, { useState } from "react";
22
import {
33
webViewRender,
4-
emit,
4+
emitToNative,
55
useNativeMessage,
6-
} from 'react-native-react-bridge/lib/web';
7-
import './example.css';
8-
import png from './Octocat.png';
9-
import {Button} from './button';
6+
} from "react-native-react-bridge/lib/web";
7+
import "./example.css";
8+
import png from "./Octocat.png";
9+
import { Button } from "./button";
1010

1111
const style = {
12-
width: '100vw',
13-
height: '100vh',
14-
margin: 'auto',
15-
backgroundColor: 'lightblue',
12+
width: "100vw",
13+
height: "100vh",
14+
margin: "auto",
15+
backgroundColor: "lightblue",
1616
};
1717

1818
const Root = () => {
19-
const [data, setData] = useState('This is Web');
20-
useNativeMessage(message => {
21-
if (message.type === 'hello') {
19+
const [data, setData] = useState("This is Web");
20+
useNativeMessage((message) => {
21+
if (message.type === "hello") {
2222
setData(message.data);
2323
}
2424
});
2525
return (
2626
<div style={style}>
2727
<div>
28-
<img src={png} width={100} height={'auto'} />
28+
<img src={png} width={100} height={"auto"} />
2929
</div>
30-
<textarea value={data} onChange={e => setData(e.target.value)} />
30+
<textarea value={data} onChange={(e) => setData(e.target.value)} />
3131
<div>
32-
<Button onClick={() => emit({type: 'hi', data: data})}>
32+
<Button onClick={() => emitToNative({ type: "hi", data: data })}>
3333
send to React Native
3434
</Button>
3535
</div>

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
* @module
55
*/
66
export type { ReactNativeMessage, WebViewMessage } from "./types";
7-
export { useWebViewMessage } from "./native";
7+
export { useWebViewMessage, emitToWebView } from "./native";

src/native/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const useWebViewMessage = <T>(
4444
/**
4545
* A function to send a message to WebView.
4646
*/
47-
export const emit = <T>(
47+
export const emitToWebView = <T>(
4848
ref: RefObject<WebView>,
4949
message: ReactNativeMessage<T>
5050
) => {

src/web/preact.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*/
66
import { useEffect } from "preact/compat";
77
import { render, ComponentChild } from "preact";
8-
import { listenNativeMessage, emitToNative, getWebViewRootElement } from "./core";
8+
import {
9+
listenNativeMessage,
10+
emitToNative,
11+
getWebViewRootElement,
12+
} from "./core";
913
import type { ReactNativeMessage } from "../types";
1014

1115
/**
@@ -28,4 +32,4 @@ export const useNativeMessage = <T>(
2832
useEffect(() => listenNativeMessage(onSubscribe), [onSubscribe]);
2933
};
3034

31-
export { emitToNative as emit };
35+
export { emitToNative };

src/web/react.spec.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @vitest-environment jsdom
22
import { afterEach, describe, expect, it } from "vitest";
33
import { cleanup, render, screen, waitFor } from "@testing-library/react";
4-
import { useNativeMessage, emit } from "./react";
4+
import { useNativeMessage, emitToNative } from "./react";
55
import { useWebViewMessage, buildEmitToWebView } from "../native";
66
import { useEffect, useState } from "react";
77
import { ReactNativeMessage, WebViewMessage } from "../types";
@@ -88,7 +88,7 @@ describe("send message to native", () => {
8888
return (
8989
<button
9090
onClick={() => {
91-
emit(message);
91+
emitToNative(message);
9292
}}
9393
>
9494
send

src/web/react.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import { ReactElement, useEffect } from "react";
77
import { render } from "react-dom";
88
import { createRoot } from "react-dom/client";
9-
import { emitToNative, getWebViewRootElement, listenNativeMessage } from "./core";
9+
import {
10+
emitToNative,
11+
getWebViewRootElement,
12+
listenNativeMessage,
13+
} from "./core";
1014
import type { ReactNativeMessage } from "../types";
1115

1216
/**
@@ -37,4 +41,4 @@ export const useNativeMessage = <T>(
3741
useEffect(() => listenNativeMessage(onSubscribe), [onSubscribe]);
3842
};
3943

40-
export { emitToNative as emit };
44+
export { emitToNative };

0 commit comments

Comments
 (0)