From 82657daa97bc708fff58ac6298143fa3f0ed6e50 Mon Sep 17 00:00:00 2001 From: Isaac Hudson Date: Thu, 10 Sep 2026 16:53:16 -0700 Subject: [PATCH] Fix cleanup for scripts with explicit scriptName --- .changeset/tidy-script-cleanup.md | 5 +++ packages/lib/src/components/Script.test.tsx | 44 +++++++++++++++++++++ packages/lib/src/hooks/use-script.tsx | 8 +++- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 .changeset/tidy-script-cleanup.md diff --git a/.changeset/tidy-script-cleanup.md b/.changeset/tidy-script-cleanup.md new file mode 100644 index 0000000..83d3914 --- /dev/null +++ b/.changeset/tidy-script-cleanup.md @@ -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. diff --git a/packages/lib/src/components/Script.test.tsx b/packages/lib/src/components/Script.test.tsx index 50e1559..675b6a8 100644 --- a/packages/lib/src/components/Script.test.tsx +++ b/packages/lib/src/components/Script.test.tsx @@ -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(); + const Container = ({ mounted }: { mounted: boolean }) => ( + + {mounted &&