Skip to content
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

fix: update provided context if readonly Prop changes #687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
24 changes: 19 additions & 5 deletions src/core/component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,42 @@ describe("core/component", () => {
const create1 = vi.fn(() => "test");
const create2 = vi.fn(() => "test");

const Component1 = createCesiumComponent<string, { children?: ReactNode }>({
const Component1 = createCesiumComponent<string, { children?: ReactNode, readOnlyProp: boolean }>({
name: "test",
create: create1,
provide: (): any => ({ context: "b" }),
provide: (element, ctx, props): any => ({ context: "b", readOnlyProp: props?.readOnlyProp }),
cesiumReadonlyProps: ["readOnlyProp"]
});

const Component2 = createCesiumComponent<string, {}>({
name: "test2",
create: create2,
});

render(
const { rerender } = render(
<Provider value={{ context: "a", context2: "foo" } as any}>
<Component1 readOnlyProp={false}>
<Component2 />
</Component1>
</Provider>,
);

await waitFor(() => {
expect(create1).toBeCalledWith({ context: "a", context2: "foo" }, expect.anything(), null);
expect(create2).toBeCalledWith({ context: "b", context2: "foo", readOnlyProp: false }, expect.anything(), null);
});

rerender(
<Provider value={{ context: "a", context2: "foo" } as any}>
<Component1>
<Component1 readOnlyProp={true}>
<Component2 />
</Component1>
</Provider>,
);

await waitFor(() => {
expect(create1).toBeCalledWith({ context: "a", context2: "foo" }, expect.anything(), null);
expect(create2).toBeCalledWith({ context: "b", context2: "foo" }, expect.anything(), null);
expect(create2).toBeCalledWith({ context: "b", context2: "foo", readOnlyProp: true }, expect.anything(), null);
});
});

Expand Down
17 changes: 13 additions & 4 deletions src/core/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export type Options<Element, Props extends RootComponentInternalProps, State = a
useRootEvent?: boolean;
};

function useForceRedraw(){
const [, setRedraw] = useState<boolean>(false)
return useCallback(() => setRedraw(a => !a),[])
}

export const useCesiumComponent = <Element, Props extends RootComponentInternalProps, State = any>(
{
name,
Expand Down Expand Up @@ -84,6 +89,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
const eventManager = ctx?.[eventManagerContextKey];
const mountReadyRef = useRef<Promise<void>>();
const unmountReadyRef = useRef<Promise<void>>();
const forceRedraw = useForceRedraw()

// Update properties
const updateProperties = useCallback(
Expand Down Expand Up @@ -160,7 +166,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
mountReadyRef.current = mount();
}
},
[], // eslint-disable-line react-hooks/exhaustive-deps
[ctx], // eslint-disable-line react-hooks/exhaustive-deps
);

const mount = useCallback(async () => {
Expand Down Expand Up @@ -210,6 +216,9 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
...ctx,
...provide(element.current, ctx, props, stateRef.current),
};
//force rerender because chaging a ref does not update the context
//changing provided to a useState will result in a rendering loop because of the useLayoutEffect below
forceRedraw()
}

const em = useRootEvent
Expand All @@ -222,7 +231,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
if (!unmountReadyRef.current) {
setMounted(true);
}
}, []); // eslint-disable-line react-hooks/exhaustive-deps
}, [ctx]); // eslint-disable-line react-hooks/exhaustive-deps

const beforeUnmount = useCallback(() => {
setMounted(false);
Expand Down Expand Up @@ -264,7 +273,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
provided.current = undefined;
stateRef.current = undefined;
element.current = undefined;
}, []); // eslint-disable-line react-hooks/exhaustive-deps
}, [ctx]); // eslint-disable-line react-hooks/exhaustive-deps

// To prevent re-execution by hot loader, execute only once
useLayoutEffect(() => {
Expand Down Expand Up @@ -305,7 +314,7 @@ export const useCesiumComponent = <Element, Props extends RootComponentInternalP
}
};
update();
}, [ctx.__$internal, mounted, props, updateProperties]);
}, [ctx, mounted, props, updateProperties]);

// Expose cesium element
useImperativeHandle(
Expand Down