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

only set a key to VNode have similar shape, fix #5618 #5622

Closed
wants to merge 1 commit into from

Conversation

pengchongfu
Copy link
Contributor

@pengchongfu pengchongfu commented May 6, 2017

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

in issue #5618 , then VNode in the first layer of the array don't get a key when father component call normalizeChildren,
but get a key when child component call normalizeChildren.

this leads to a changeable key, and component was re-created.

@yyx990803 yyx990803 self-requested a review May 6, 2017 16:03
Copy link
Member

@yyx990803 yyx990803 left a comment

Choose a reason for hiding this comment

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

Thanks!

  • Flow definition on line 39 needs to be updated (the argument is no longer optional)
  • We can use a shorter initial index string (like 0)

@pengchongfu
Copy link
Contributor Author

test still failed, I am try to find the reason.

@gebilaoxiong
Copy link
Member

gebilaoxiong commented May 6, 2017

@pengchongfu

You can compile the code, and then copy the test case to the console debugging

@pengchongfu
Copy link
Contributor Author

pengchongfu commented May 7, 2017

@gebilaoxiong , thanks.

  • My change seems to introduce a bug : (. I hava test locally and found this test failed.

  • I make a demo, I know it is a little hack. compVm._watcher was inactive, I am try to find the reason.

my fault, compVm._watcher changed. see this demo

@pengchongfu pengchongfu changed the title always set a key to VNode when call normalizeChildren, fix #5618 only set a key to VNode have similar shape, fix #5618 May 7, 2017
@pengchongfu
Copy link
Contributor Author

@yyx990803 , my change would introduce new bug :(, because if we force every VNode has a key by order, this will have side effect for patch, see this demo.

I have update my change, maybe we should filter the VNode before set a key.
only set a key to VNode have similar shape, likely generated by v-for.

@@ -45,7 +45,11 @@ function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNo
last = res[res.length - 1]
// nested
if (Array.isArray(c)) {
res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
Copy link
Member

Choose a reason for hiding this comment

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

I think here only need to determine whether c is created by slot, avoid to set key

Copy link
Member

Choose a reason for hiding this comment

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

Let me fix it 🙂

@gebilaoxiong
Copy link
Member

gebilaoxiong commented May 9, 2017

hi, I looked at your code

there is a problem about the nested child as a v-for created

<div id="app"></div>
const vm = new Vue({
  el: '#app',
  data: { n: 1 },
  render(h) {
    const list = []

    for (let i = 0; i < this.n; i++) {
      list.push(h('input', {
        attrs: {
          value: 'a',
          type: 'text'
        }
      }))
    }

    const b = h('input', {
      attrs: {
        value: 'b',
        type: 'text'
      }
    })
    
    return h('div', [
      [...list, b]
    ])
  }
})

If the nested list is followed by the same type of element...

steps to reproduce

Enter some data in the last input box

modify the value of n in the console :

vm.n++

What is expected

it will create a new input, and the value of this input is b

What is actually happening?

But the correct should be create a element In front of b

Conclusion

It is always problematic to distinguish whether the element is a loop

So I think here should only set the key for v-for created, or should remove this code

to avoid other situations

@pengchongfu
Copy link
Contributor Author

@gebilaoxiong , you are right, the best way is coding carefully...

@pengchongfu pengchongfu closed this May 9, 2017
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.

None yet

3 participants