Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fixtures/app-export-default-react.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { webViewCreateRoot } from "react-native-react-bridge/lib/web";
import Comp from "./Component";
import "./example.css";

const App = () => {
return <Comp />;
};

export default webViewCreateRoot(App);
246 changes: 126 additions & 120 deletions src/plugin/__snapshots__/index.spec.ts.snap

Large diffs are not rendered by default.

99 changes: 54 additions & 45 deletions src/plugin/__snapshots__/metro.spec.ts.snap

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/plugin/babel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ describe("isEntryFile", () => {
const paths = isEntryFile(src, filename);
expect(paths).toEqual(true);
});

it("export default webViewRender(App) (react createRoot)", () => {
const filename = "app-export-default-react.jsx";
const src = readFixture(filename);
const paths = isEntryFile(src, filename);
expect(paths).toEqual(true);
});
});
9 changes: 9 additions & 0 deletions src/plugin/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export const isEntryFile = (src: string, filename: string) => {
name: "webViewRender",
},
},
}) ||
looksLike(path.node, {
declaration: {
type: "CallExpression",
callee: {
type: "Identifier",
name: "webViewCreateRoot",
},
},
})
) {
isEntry = true;
Expand Down
2 changes: 1 addition & 1 deletion src/web/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { webViewRender, emit, useNativeMessage } from "./react";
export * from "./react";
9 changes: 9 additions & 0 deletions src/web/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { ReactElement, useEffect } from "react";
import { render } from "react-dom";
import { createRoot } from "react-dom/client";
import { emit, getWebViewRootElement, listenNativeMessage } from "./core";
import type { Message } from "../types";

Expand All @@ -19,6 +20,14 @@ export const webViewRender = (root: ReactElement): string => {
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.
*/
Expand Down