-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
react.ts
40 lines (36 loc) · 1.08 KB
/
react.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Modules for React DOM
*
* @module
*/
import { ReactElement, useEffect } from "react";
import { render } from "react-dom";
import { createRoot } from "react-dom/client";
import { emitToNative, getWebViewRootElement, listenNativeMessage } from "./core";
import type { ReactNativeMessage } from "../types";
/**
* The entry point of web file
*
* This statement is detected by babelTransformer as an entry point
* All dependencies are resolved, compressed and stringified into one file
*/
export const webViewRender = (root: ReactElement): string => {
render(root, getWebViewRootElement());
return ""; // dummy
};
/**
* {@link webViewRender} but initiated with React's createRoot
*/
export const webViewCreateRoot = (root: ReactElement): string => {
createRoot(getWebViewRootElement()).render(root);
return ""; // dummy
};
/**
* A hook to subscribe messages from React Native.
*/
export const useNativeMessage = <T>(
onSubscribe: (message: ReactNativeMessage<T>) => void
) => {
useEffect(() => listenNativeMessage(onSubscribe), [onSubscribe]);
};
export { emitToNative as emit };