Skip to content

Commit

Permalink
fix: prevent unnecessary re-mount (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 authored Aug 20, 2024
1 parent 9c95c87 commit 335083e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/Globe/Globe.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { action } from "@storybook/addon-actions";
import { Meta, StoryObj } from "@storybook/react";
import { CesiumTerrainProvider, EllipsoidTerrainProvider, IonResource } from "cesium";
import { StrictMode } from "react";

import Viewer from "../Viewer";

import Globe from "./Globe";
import { StrictMode } from "react";

type Story = StoryObj<typeof Globe>;

Expand All @@ -28,16 +28,20 @@ export const Basic: Story = {
};

export const Promise: Story = {
args: { enableLighting: true },
render: args => (
args: { enableLighting: true, isTerrainEnabled: false } as any,
render: ({ isTerrainEnabled, ...args }: any) => (
<StrictMode>
<Viewer full>
<Globe
{...args}
terrainProvider={CesiumTerrainProvider.fromUrl(IonResource.fromAssetId(1), {
requestVertexNormals: true,
requestWaterMask: false,
})}
terrainProvider={
isTerrainEnabled
? CesiumTerrainProvider.fromUrl(IonResource.fromAssetId(1), {
requestVertexNormals: true,
requestWaterMask: false,
})
: new EllipsoidTerrainProvider()
}
onImageryLayersUpdate={action("onImageryLayersUpdate")}
onTerrainProviderChange={action("onTerrainProviderChange")}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/core/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
const initialProps = useRef<Props>(propsWithChildren(props));
const prevProps = useRef<Props>({} as Props);
const [mounted, setMounted] = useState(false);
const mountedRef = useRef(false);
const wrapperRef = useRef<HTMLDivElement>(null);
const stateRef = useRef<State>();
const eventManager = ctx?.[eventManagerContextKey];
Expand Down Expand Up @@ -145,7 +146,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
initialProps.current = props;

// Recreate cesium element when any read-only prop is updated
if (updatedReadonlyProps.length > 0) {
if (mountedRef.current && updatedReadonlyProps.length > 0) {
if (process.env.NODE_ENV !== "production") {
console.warn(
`Warning: <${name}> is recreated because following read-only props have been updated: ${updatedReadonlyProps.join(
Expand Down Expand Up @@ -254,6 +255,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
provided.current = undefined;
stateRef.current = undefined;
element.current = undefined;
mountedRef.current = false;
}, []); // eslint-disable-line react-hooks/exhaustive-deps

// To prevent re-execution by hot loader, execute only once
Expand Down Expand Up @@ -290,6 +292,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
// first time
prevProps.current = propsWC;
initialProps.current = propsWC;
mountedRef.current = true;
}
};
update();
Expand Down

0 comments on commit 335083e

Please sign in to comment.