Skip to content

feat(bridge-react): add support for custom createRoot on createBridgeComponent #3544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-eagles-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/bridge-react': minor
---

feat(bridge-react): Added support for custom `createRoot` on `createBridgeComponent`
32 changes: 32 additions & 0 deletions packages/bridge/bridge-react/__tests__/bridge.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,36 @@ describe('bridge', () => {
expect(getHtml(container)).toMatch('hello world');
expect(ref.current).not.toBeNull();
});

it('createRemoteComponent with custom createRoot prop', async () => {
const renderMock = vi.fn();

function Component({ props }: { props?: Record<string, any> }) {
return <div>life cycle render {props?.msg}</div>;
}
const BridgeComponent = createBridgeComponent({
rootComponent: Component,
createRoot: () => {
return {
render: renderMock,
unmount: vi.fn(),
};
},
});
const RemoteComponent = createRemoteComponent({
loader: async () => {
return {
default: BridgeComponent,
};
},
fallback: () => <div></div>,
loading: <div>loading</div>,
});

const { container } = render(<RemoteComponent />);
expect(getHtml(container)).toMatch('loading');

await sleep(200);
expect(renderMock).toHaveBeenCalledTimes(1);
});
});
13 changes: 10 additions & 3 deletions packages/bridge/bridge-react/src/provider/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ErrorBoundary } from 'react-error-boundary';
import { RouterContext } from './context';
import { LoggerInstance } from '../utils';
import { federationRuntime } from './plugin';
import { createRoot } from './compat';
import { createRoot as defaultCreateRoot } from './compat';

type RenderParams = RenderFnParams & {
[key: string]: unknown;
Expand All @@ -17,17 +17,24 @@ type DestroyParams = {
moduleName: string;
dom: HTMLElement;
};
type RootType = HTMLElement | ReturnType<typeof createRoot>;
type RootType = HTMLElement | ReturnType<typeof defaultCreateRoot>;

export type ProviderFnParams<T> = {
rootComponent: React.ComponentType<T>;
render?: (
App: React.ReactElement,
id?: HTMLElement | string,
) => RootType | Promise<RootType>;
createRoot?: (
container: Parameters<typeof defaultCreateRoot>[0],
options?: Parameters<typeof defaultCreateRoot>[1],
) => ReturnType<typeof defaultCreateRoot>;
};

export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
export function createBridgeComponent<T>({
createRoot = defaultCreateRoot,
...bridgeInfo
}: ProviderFnParams<T>) {
return () => {
const rootMap = new Map<any, RootType>();
const instance = federationRuntime.instance;
Expand Down