Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: instance children array would desync from object children array #3490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

krispya
Copy link
Member

@krispya krispya commented Mar 9, 2025

This guarantees that the React instance children array does not desync from the Three object chidlren array. However, it is computationally expensive to be searching all of these arrays each time so I am keen to think of ways to optimize these operations.

Copy link

codesandbox-ci bot commented Mar 9, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c6bbc0f:

Sandbox Source
example Configuration

@krispya
Copy link
Member Author

krispya commented Mar 31, 2025

@CodyJasonBennett I wanted to come back to this as desyncs currently happen whenever a list is updated. I know we weren't happy to turn the push into an insert, but this emulates the DOM behavior of appendChild where it moves the child to the end of the array if it is already a child: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

Maybe we can think of a faster algorithm?

Comment on lines -340 to 348

// Link instances
child.parent = parent

// Remove existing child if it exists
const existingIndex = parent.children.indexOf(child)
if (existingIndex !== -1) parent.children.splice(existingIndex, 1)

// Append child
parent.children.push(child)
Copy link
Member

@CodyJasonBennett CodyJasonBennett Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3490 (comment) Maybe we can move the check before mutating anything.

// Remove existing child if it exists (re-order to last)
if (child.parent === parent) {
  const existingIndex = parent.children.indexOf(child)
  if (existingIndex !== -1) parent.children.splice(existingIndex, 1)
}

// Link instances
child.parent = parent

// Append child
parent.children.push(child)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this makes sense. I think further optimizations would require convincing React to provide a path for append separate from reorder since it has this information internally. However, for now we can probably not worry about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants