React总结7:Each child in an array or iterator should have a unique "key" prop.的处理方法
来源:互联网 发布:c语言教材 kindle 编辑:程序博客网 时间:2024/05/14 06:44
PS:当你在写react的时候报了类似于这样子的错:Each child in an array or iterator should have a unique “key” prop.
解决办法只要在循环的每个子项添加一个key就行了,代码如下:
var names = ['Alice', 'Emily','Kate'];ReactDOM.render( <div> { names.map(function (name, key) { return <div key={key}>Hello, {name}!</div> }) } </div>, document.getElementById('example'));
原因如下:
react的key关乎到react的dom-diff算法 react中对于dom的操作是根据生成的data-reactid进行绑定的,添加key可以保证dom结构的完整性,而不会根据react自己对dom标记的key进行重新分配 react每次决定重新渲染的时候,几乎完全是根据data-reactid来决定的,最重要的是这个机制
dom-diff是指: 所有的 DOM 变动,都先在虚拟 DOM 上发生,然后再将实际发生变动的部分,反映在真实 DOM上。它可以极大提高网页的性能表现。
7.20补:
key是专门为React服务的,但是它不会传递给你的组件,也就是说,在组件中,你无法通过props.key来获取它的值,但是它又绑定到了组件中,作为组件的属性而存在,然而,你无法通过this.key,或者是通过参数获取它,它是React特殊处理的。
如果你想获得key的值只能另外设置一个值,然后通过props属性对象获取key的值,如下:
const content = posts.map((post) => <Post key={post.id} id={post.id} title={post.title} />);
另外标识一个id属性来传递key的值。
key值的唯一性
兄弟元素之间的key值必须是唯一的,当然不是全局唯一,只是对当前作用域的相关兄弟元素。
function Blog(props) { const sidebar = ( <ul> {props.posts.map((post) => <li key={post.id}> {post.title} </li> )} </ul> ); const content = props.posts.map((post) => <div key={post.id}> <h3>{post.title}</h3> <p>{post.content}</p> </div> ); return ( <div> {sidebar} <hr /> {content} </div> );}
阅读全文
0 0
- React总结7:Each child in an array or iterator should have a unique "key" prop.的处理方法
- react Warning: Each child in an array or iterator should have a unique "key" prop. Check the render
- react-native中debug报错信息:Each child in an array or iterator should have a unique "key" prop.
- Warning: Each child in an array or iterator should have a unique "key" prop.
- Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...
- Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...
- ReactNativeiOS(三)开发零碎3 warning: Each child in ar array or integrator should have a unique "key" prop
- ant-design Warning: Each record in table should have a unique `key` prop,or set `rowKey` to an un
- Cannot add or update a child row: a foreign key constraint fails 问题的处理
- Must specify unique android:id, android:tag, or have a parent with an id for XXX
- Shuffle an Array or a List - Algorithm in Java
- react native 错误:Make sure you have an Android emulator running or a device connected and have set up
- Each module has to have a unique path
- There's an iterator stored in with each hash
- Have a first child
- opencv中关于图像图像处理时出现Bad argument (Array should be the cvMat or IplImage) in unknown function ……错误的解决办法
- Should new index columns be in the key, or included?
- Should I Use a Property or an Instance Variable?
- 数据结构之分解质因数
- java和python中for循环的差异
- 数据库模型设计【表设计】
- ai域名为什么突然那么多人注册?
- Java Virtual Machine Launcher: A Java Exception has occurred
- React总结7:Each child in an array or iterator should have a unique "key" prop.的处理方法
- 波束形成算法
- 《剑指offer》二叉树镜像
- 考前XJB打暴力系列
- macOS Sierra安装HomeBrew来执行更多Linux命令
- 《我所理解的Cocos2d-x》书评及笔记
- 51nod 1769 Clarke and math2
- KCF目标跟踪方法分析与总结
- Html基础知识