Skip to content

Bug: re-ordering components with stable keys invalidates refs/state, since 19.0.0 #34307

Closed as not planned

Description

@rain-sk

I've tested with 18.3.1, 19.0.0, and 19.1.1.

I build a modular synth application which uses the Web Audio API. Since it's possible for synth patches to grow quite large, I memoize the ordered list of modules for the sake of having a sensible keyboard navigation order. This means that the order of rendered modules can change.

Up until React 18.3.1, this changing order of rendered modules was not causing any issues. I make sure to use stable unique identifiers as keys when rendering, so components' internal state remains stable as the modules are dragged around the canvas.

Here's a snippet of the code used to render the modules:

	const sortedModules = useMemo(
		() => {
			// ... order the modules based on their positions in the canvas
		}),
		[state.modulePositions, state.modules],
	);

	return (
		<>
			{sortedModules.map(([module, position]) => (
				<Module
					key={module.moduleKey}
					module={module}
					position={position}
					state={state}
					dispatch={dispatch}
				/>
			))}
		</>
	);

Internally, <Module /> calls a hook which stores a ref to a "node" object – which acts as its handle into the audio API – and initializes it like so:

	// ...
	const nodeRef = useRef<NodeType>(undefined);

	if (!nodeRef.current) {
		nodeRef.current = nodeFactory();
	}
	// ...

So, since React 19.0.0, this ref seems to get out of sync when the order of the rendered components changes.

React version: 19.0.0, 19.1.1

Steps To Reproduce

  1. git clone https://github.com/rain-sk/synth.kitchen.git && cd synth.kitchen
  2. git checkout demo-react19-issues
  3. cd app/web
  4. npm i && npm run dev
  5. Visit localhost:8080 and press "start"
  6. Double-click in the center of the screen, then choose one of the options in the menu to add a new module to the canvas
  7. Drag the output module to the bottom-right corner of the screen, past the newly-added module

Expected:

  • internal module state is stable regardless of render order

Result:

  • hit the debugger line in the useNode hook, indicating that the ref was reset, even though we've already initialized the corresponding node

Note: the commit behind the HEAD of demo-react19-issues is the one which adds the debugger statement. Switching to that commit (b8f76f9f845328a6e83fdd3a13981f82cf16fe11) and re-installing node_modules, demonstrates that this problem did not exist prior to React 19.

Link to code example:

The current behavior

Changing render order of components with stable keys results in broken internal refs.

The expected behavior

Internal refs of react components are stable regardless of render order, given stable key props.

Activity

  1. added
    Status: Unconfirmed
    on Aug 26, 2025
  2. rain-sk commented on Aug 26, 2025

    @rain-sk
    Author

    As a note, I tried switching from useRef to useState for storing the node, but this leads to a whole other kind of crash caused by an InvalidAccessException. Triggering this crash requires manually re-ordering the modules three times.

    Applying the same ref->state change with react and react-dom at 18.3.1, there is no crash.

  3. rain-sk commented on Aug 26, 2025

    @rain-sk
    Author

    This seems to be a duplicate of #32561, or at least they seem related.

  4. changed the title [-]Bug: key instability since 19.0.0[/-] [+]Bug: re-ordering components with stable keys invalidates refs/state, since 19.0.0[/+] on Aug 26, 2025
  5. rain-sk commented on Aug 26, 2025

    @rain-sk
    Author

    My opinion: the changes that came with <StrictMode> with React 18 have been helpful in finding certain kinds of bugs, but when working with the Web Audio API, it makes React a pain to use as a framework.

  6. eps1lon commented on Aug 27, 2025

    @eps1lon
    Collaborator

    Can you reproduce this with a production build as well? We have a similar issue that only happens in dev with StrictMode: #32561

  7. rain-sk commented on Aug 27, 2025

    @rain-sk
    Author

    @eps1lon I cannot reproduce in production. This is only in dev with StrictMode.

  8. github-actions commented on Dec 1, 2025

    @github-actions

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

  9. added
    Resolution: Stale
    on Dec 1, 2025
  10. rain-sk commented on Dec 2, 2025

    @rain-sk
    Author

    I have updated the example branch mentioned in the bug summary to test with React 19.2.0 and the issue persists.

  11. removed
    Resolution: Stale
    on Dec 4, 2025
  12. github-actions commented on Mar 4, 2026

    @github-actions

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

  13. added
    Resolution: Stale
    on Mar 4, 2026
  14. github-actions commented on Mar 11, 2026

    @github-actions

    Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

  15. rain-sk commented on Nov 26, 2026

    @rain-sk
    Author

    I can no longer reproduce this bug since 19.3.1..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleStatus: Unconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions