-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
Conversation
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:
|
@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 Maybe we can think of a faster algorithm? |
|
||
// 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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
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.