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: 5 additions & 0 deletions .changeset/tidy-script-cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@playcanvas/react": patch
---

Respect explicit static scriptName values when cleaning up Script components so unmounting removes the registered script and remounting avoids duplicate-script warnings. Preserve class-name fallback cleanup for scripts without an explicit name.
44 changes: 44 additions & 0 deletions packages/lib/src/components/Script.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,50 @@ describe('Script Component', () => {
});

describe('Cleanup', () => {
class _OrbitCamera extends PcScript {
static scriptName = 'orbitCamera';
}

class FallbackScript extends PcScript {}

it.each([
['explicit scriptName', _OrbitCamera, 'orbitCamera'],
['class-name fallback', FallbackScript, 'fallbackScript']
] as const)('should clean up and remount using %s', async (_label, script, scriptName) => {
const scriptRef = React.createRef<PcScript>();
const Container = ({ mounted }: { mounted: boolean }) => (
<Application deviceTypes={['null']}>
<Entity>{mounted && <Script script={script} ref={scriptRef} />}</Entity>
</Application>
);

const { rerender } = render(<Container mounted />);
await waitFor(() => expect(scriptRef.current).toBeInstanceOf(script));
const firstInstance = scriptRef.current!;
const entity = firstInstance.entity;
expect(entity.script![scriptName]).toBe(firstInstance);

rerender(<Container mounted={false} />);
expect(scriptRef.current).toBeNull();
expect(entity.script).toBeDefined();
expect(entity.script![scriptName]).toBeUndefined();

const warnSpy = vi.spyOn(console, 'warn');
try {
rerender(<Container mounted />);
await waitFor(() => expect(scriptRef.current).toBeInstanceOf(script));
expect(scriptRef.current).not.toBe(firstInstance);
expect(scriptRef.current!.entity).toBe(entity);
expect(entity.script![scriptName]).toBe(scriptRef.current);
expect(warnSpy).not.toHaveBeenCalled();
} finally {
warnSpy.mockRestore();
}

rerender(<Container mounted={false} />);
expect(entity.script![scriptName]).toBeUndefined();
});

it('should clean up script instance on unmount', async () => {
const destroySpy = vi.fn();

Expand Down
8 changes: 6 additions & 2 deletions packages/lib/src/hooks/use-script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ import { useParent } from './use-parent.tsx';
* myProperty: 'value',
* });
*/
export const useScript = (scriptConstructor: SubclassOf<Script>, props: Props, ref: ForwardedRef<Script>): void => {
export const useScript = (
scriptConstructor: SubclassOf<Script> & { scriptName?: string },
props: Props,
ref: ForwardedRef<Script>
): void => {
const parent: Entity = useParent();
const app: Application = useApp();
const scriptName: string = toLowerCamelCase(scriptConstructor.name);
const scriptName: string = scriptConstructor.scriptName || toLowerCamelCase(scriptConstructor.name);
const scriptRef = useRef<Script | null>(null);
const scriptComponentRef = useRef<ScriptComponent | null>(null);

Expand Down