playcanvas / playcanvas/react

Script cleanup uses the class name instead of the registered scriptName

Open Beginner friendly
#343 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
528
Forks
60
Avg merge
1m
Merged PRs (30d)
1

Description

Description

useScript creates scripts using their constructor but destroys them using a name derived from scriptConstructor.name:

const scriptName = toLowerCamelCase(scriptConstructor.name);

This can differ from the name PlayCanvas uses to register the script when the constructor defines a static scriptName.

Bundlers can expose this mismatch. In my Vite development build, OrbitCamera becomes _OrbitCamera, while its static scriptName remains orbitCamera. Cleanup therefore calls destroy('_OrbitCamera'), which fails to remove the registered script.

Steps to reproduce
  1. Mount <OrbitControls /> on a camera in a Vite application.
  2. Conditionally unmount the controls while keeping the camera entity alive.
  3. Mount another camera controller on the same camera.

In my application, the globe orbit controls are unmounted before a separate part of the scene takes over the shared camera. But because the orbit script survives unmounting, the orbit controller continues writing the camera transform, competing with the new controller.

Remounting also produces warnings such as:

script 'orbitCamera' is already added to entity 'camera'

React StrictMode's effect cleanup/remount cycle exposes these duplicate-script warnings as well.

Environment
  • @playcanvas/react: 0.11.5
  • PlayCanvas: 2.21.3
  • React: 19
  • Vite: 6
  • Desktop Chrome
Proposed fix

Prefer the constructor's explicit scriptName, retaining the existing class-name fallback:

const scriptName = scriptConstructor.scriptName || toLowerCamelCase(scriptConstructor.name);

I verified the mismatch with a PlayCanvas script whose class name is _OrbitCamera and whose static scriptName is orbitCamera: destroying _OrbitCamera returns false and leaves the script attached; destroying orbitCamera succeeds.

Found with the assistance of GPT-6 Astra, but verified manually.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the useScript implementation described in the issue and inspect how it derives the cleanup name from the constructor. Verify cleanup uses the explicit static scriptName when present while preserving the class-name fallback, then reproduce the OrbitControls mount, unmount, and remount scenario to confirm the script is removed without duplicate-script warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.