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

Why shouldn't we use :key="index" in v-for #94

Open
xianshenglu opened this issue May 30, 2019 · 0 comments
Open

Why shouldn't we use :key="index" in v-for #94

xianshenglu opened this issue May 30, 2019 · 0 comments

Comments

@xianshenglu
Copy link
Owner

xianshenglu commented May 30, 2019

demo1

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .app {
        width: 90vh;
        height: 80vh;
        padding: 15px;
        background: #eee;
        margin: 10vh auto;
        overflow: auto;
      }
      .app__button {
        margin: 0 auto;
      }
      .app__checkbox {
        margin: 10px;
        display: block;
      }
    </style>
  </head>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.js"></script>
    <div id="app" class="app">
      <el-button class="app__button" @click="unshiftOption"
        >unshiftOption
      </el-button>
      <el-checkbox
        class="app__checkbox"
        v-for="(item,index) in options"
        :key="index"
        v-model="item.checked"
        >{{ item.label }}</el-checkbox
      >
    </div>
    <script>
      new Vue({
        el: '#app',
        data: {
          value: '',
          options: [
            { label: 'label1', name: 'checkbox1', checked: undefined },
            { label: 'label2', name: 'checkbox2', checked: undefined },
            { label: 'label3', name: 'checkbox3', checked: undefined }
          ],
          counter: 4
        },
        created() {
          window.vm = this
        },
        methods: {
          unshiftOption() {
            const count = this.counter++
            this.options.unshift({
              label: 'label' + count,
              name: 'checkbox' + count,
              checked: undefined
            })
          }
        }
      })
    </script>
  </body>
</html>

demo2

In this case, append a new item doesn't run the data function. So, it shows the wrong value. Normally, it should run the data function to get a new state.

Solution

Bind key to unique id would solve this problem. For example, in demo1 we can

:key="name"

And in demo2, we can

:key="i"

Performance

todo....

Reference

@xianshenglu xianshenglu changed the title Why not using :key="index" Why not using :key="index" in v-for May 30, 2019
@xianshenglu xianshenglu changed the title Why not using :key="index" in v-for Why shouldn't we use :key="index" in v-for May 30, 2019
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

1 participant