diff --git a/.changeset/tidy-script-cleanup.md b/.changeset/tidy-script-cleanup.md new file mode 100644 index 00000000..05ff3e76 --- /dev/null +++ b/.changeset/tidy-script-cleanup.md @@ -0,0 +1,5 @@ +--- +"@playcanvas/react": patch +--- + +Fix Script cleanup for scripts whose class name differs from their registered name (such as an explicit static `scriptName` after bundling or minification), so unmounting removes the script and remounting no longer warns about a duplicate. diff --git a/packages/lib/src/components/Script.test.tsx b/packages/lib/src/components/Script.test.tsx index 50e1559e..675b6a8f 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 &&