Skip to content

KeyboardControls onChange callback retains stale closure when dependencies change #2578

@dammafra

Description

@dammafra

Problem description:

The onChange callback passed to the KeyboardControls component becomes stale and continues to refer to the initial function closure.

This occurs because the internal event listener registration within KeyboardControls is missing the onChange prop from its useEffect dependency array.

See https://github.com/pmndrs/drei/blob/master/src/web/KeyboardControls.tsx#L111

Environment:

  • three version: 0.180.0
  • @react-three/fiber version: 10.7.6
  • @react-three/drei version: 9.3.0
  • node version: v22.18.0
  • npm version: 10.9.3

Relevant code:

See https://codesandbox.io/p/sandbox/quizzical-water-6yy5tn

Suggested solution:

Add onChange to the above usEffect deps array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions