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 &&