react / react/react

Bug: deleting structure via DOM API breaks state updates

Open
#30,444 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: DOM Type: Discussion
Dominant language
JavaScript
Stars
251k
Forks
51.4k
Avg merge
2d 4h
Merged PRs (30d)
53

Description

React version: 18.2.0

Steps To Reproduce

  1. have a node with a nested structure that embeds state
  2. change that nodes innerHTML, e.g. by setting it empty
  3. update the state
  4. updated state does not restore DOM Nodes

Link to code example:

https://codesandbox.io/p/sandbox/react-webcomponents-69d8v8

The current behavior

DOM stays empty, i.e. rerenders have no effect

The expected behavior

DOM should be re-populated, i.e. react should take control again.

Context

I can kind of understand why react "breaks" but the issue here is that by e.g. using webcomponents that integrate with forms and implement a "reset" behaviour the applied solution will most likely involve some kind of innerHTML setting. I wish react wouldn't just "give up" in such cases. Is there a workaround possible here?

Contributor guide

Open the contributing guide

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 by reproducing the behavior in the linked CodeSandbox with React 18.2.0: clear a nested node's innerHTML through the DOM API, then update state. Read the React DOM handling involved in reconciling externally deleted nodes; done means a state update repopulates the DOM while retaining the web-component reset use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.