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

New method for implicit keys, traverseAllChildren #2378

Closed
syranide opened this issue Oct 18, 2014 · 3 comments
Closed

New method for implicit keys, traverseAllChildren #2378

syranide opened this issue Oct 18, 2014 · 3 comments

Comments

@syranide
Copy link
Contributor

The current state of implicit keys in React, psuedo-JSX:

1. <div .0><span .0.0 /></div>
2. <div .0>[<span .0.0 />]</div> -- GOOD reconciles .0.0

1. <div .0><span .0.0 /></div>
2. <div .0>[<span .0.0 /><span .0.1 />]</div> -- GOOD reconciles .0.0

1. <div .0><span .0.0 /><span .0.1 /></div>
2. <div .0>[<span .0.0:0 />]<span .0.1 /></div> -- BAD destroys .0.0

1. <div .0>[<span .0.0 />]</div>
2. <div .0>[[<span .0.0:0 />]]</div> -- BAD destroys .0.0

I propose Array/Set should ignore tailing sub-array indices with value 0 and if the root is an only child then it has index 0 (this is currently the case, except it's broken for objects):

.0.0 => .0.0
.0.0:0:1:0:0 => .0.0:0:1
.0.0:0:1:0:0:1:0:0 => .0.0:0:1:0:0:1

The first ReactNode in every Array now reconciles from and to any depth. If we want to improve Maps to reconcile in the same way then all previous indices should be discarded when a Map is encountered (possibly discarding the entire "computed name so far" even).

Pseudo-JSX showing how the new indices would be computed ([...] is discarded), all the examples at the top now reconcile correctly.

<div .0>
  [
    [
      <span .0.0[:0:0] />
        [
          <span .0.0:0:1[:0] />
            [
              [
                <span .0.0:0:1:1[:0:0] />
                <span .0.0:0:1:1:0:1 />
              ]
            ]
          <span .0.0:0:2 />
        ]
      <span .0.0:1 />
    ]
  <span .0.1 />
</div>

cc @sebmarkbage

@sophiebits
Copy link
Collaborator

Can you give example code for render where this would make an improvement?

@syranide
Copy link
Contributor Author

@spicyj React's optimization to drop the array allocation for 0-1 children only reconciles correctly at the lowest level. This PR allows it to work at any depth and thus extends the same courtesy to other use-cases.

Is it common? No (though it can be useful, also compare ex. 1 and 3), but I don't see that as a justification for making it inconsistent unless there's a measurable cost that we're not willing to take, but I'm quite sure there isn't.

@gaearon
Copy link
Collaborator

gaearon commented Oct 1, 2017

I’ll close this as stale.
If somebody needs this please create a new issue describing the use case in more detail.

@gaearon gaearon closed this as completed Oct 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants