问题 1:为什么在写 React/Vue 项目时要在列表组件中写 key,它的作用是什么?
作者: fingerpan创建于 2019年1月21日更新于 2026年6月22日
标签VueReact
// The following code is formatted for readability // oldCh is an array of old virtual nodes if (isUndef(oldKeyToIdx)) { oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx) } if (isDef(newStartVnode.key)) { // Get the index using the map method idxInOld = oldKeyToIdx[newStartVnode.key] } else { // Get the index using the traversal method idxInOld = findIdxInOld(newStartVnode, oldCh, oldStartIdx, oldEndIdx) }
内容来源: Advanced-Frontend/Daily-Interview-Question