Skip to content
Open
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: 4 additions & 1 deletion src/hooks/__tests__/useRemoteComponent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { createUseRemoteComponent } from "../useRemoteComponent";
import React from "react";

jest.mock("react", () => ({
useEffect: jest.fn(f => f())
useEffect: jest.fn(f => f()),
useRef: jest.fn(s => ({
current: s
}))
}));

const waitNextFrame = () => new Promise(resolve => setTimeout(resolve));
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useRemoteComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
import createLoadRemoteModule from "@paciolan/remote-module-loader";

export interface UseRemoteComponentHook {
Expand All @@ -16,10 +16,12 @@ export const createUseRemoteComponent = (
err: undefined,
component: undefined
});
const latestUrl = useRef(url);

useEffect(() => {
let update = setState;
update({ loading: true, err: undefined, component: undefined });
latestUrl.current = url;
loadRemoteModule(url)
.then(module =>
update({ loading: false, err: undefined, component: module.default })
Expand All @@ -33,7 +35,9 @@ export const createUseRemoteComponent = (
};
};
}, [url]);

if (latestUrl.current !== url) {
return [true, undefined, undefined];
}
return [loading, err, component];
};

Expand Down