function NumberList(props) {
const numbers = props.numbers;
const listItems = numbers.map((number) =>
<li key={number.toString()}>
{number}
</li>
);
return (
<ul>{listItems}</ul>
);
} const numbers = [1, 2, 3, 4, 5];
ReactDOM.render(
<NumberList numbers={numbers} />,
document.getElementById('root')
);

keys用于给数组中元素一个稳定的标识。选取key得最好方法是选择能够唯一标识这个item的字符串。如果没有独特的标识,在万不得已的情况下可以选择item的index作为key值。

const todoItems = todos.map((todo, index) =>
// Only do this if items have no stable IDs
<li key={index}>
{todo.text}
</li>
);

key值只有在数组中才有意义。务必 把 key添加到子级数组里组件本身上,而不是每个子级内部最外层 HTML 上:

For example, if you extract a ListItem component, you should keep the key on the<ListItem /> elements in the array rather than on the root <li> element in the ListItemitself.

// incorrect key usage
function ListItem(props) {
const value = props.value;
return (
// Wrong! There is no need to specify the key here:
<li key={value.toString()}>
{value}
</li>
);
} function NumberList(props) {
const numbers = props.numbers;
const listItems = numbers.map((number) =>
// Wrong! The key should have been specified here:
<ListItem value={number} />
);
return (
<ul>
{listItems}
</ul>
);
} const numbers = [1, 2, 3, 4, 5];
ReactDOM.render(
<NumberList numbers={numbers} />,
document.getElementById('root')
);
// correct
function ListItem(props) {
// Correct! There is no need to specify the key here:
return <li>{props.value}</li>;
} function NumberList(props) {
const numbers = props.numbers;
const listItems = numbers.map((number) =>
// Correct! Key should be specified inside the array.
<ListItem key={number.toString()}
value={number} />
);
return (
<ul>
{listItems}
</ul>
);
} const numbers = [1, 2, 3, 4, 5];
ReactDOM.render(
<NumberList numbers={numbers} />,
document.getElementById('root')
);

一般的做法是,将key用在map()函数中。key值不会传递给组件,如果你的组件需要同样的值,明确的将其定义为prop,并且使用其他的名字

const content = posts.map((post) =>
<Post
key={post.id}
id=
{post.id}
title={post.title} />
);

注意:数字型属性会按大小排序并且排在其它属性前面。一旦发生这种情况,React 渲染组件的顺序就是混乱。可能在 key 前面加一个字符串前缀来避免:

render() {
var items = {}; this.props.results.forEach(function(result) {
// 如果 result.id 看起来是一个数字(比如短哈希),那么
// 对象字面量的顺序就得不到保证。这种情况下,需要添加前缀
// 来确保 key 是字符串。
items['result-' + result.id] = <li>{result.text}</li>;
}); return (
<ol>
{items}
</ol>
);
}

最新文章

  1. 关于zepto(相似于jquery、jQuery用于网页浏览器,zepto用于手机浏览器)
  2. Newtonsoft.Json解析Json字符串案例:
  3. android开发中获取&lt;meta-data&gt;数据
  4. ASP.NET WebAPI 09 Controller的激活
  5. Fuzzy test
  6. 设置html滚动条(陶庭飞问题)
  7. SFTP文件上传与下载
  8. C#,.net获取字符串中指定字符串的个数、所在位置与替换字符串
  9. TVS和一般的稳压二极管有什么区别
  10. 1.phpStrom连接远程代码
  11. css区分ie8/ie9/ie10/ie11 chrome firefox的代码
  12. js 颜色选择插件
  13. canvas意料之外获得降龙十八掌的效果
  14. springboot(十九):SpringBoot+EHcache实现缓存
  15. HTML禁止右键复制【两行代码实现】
  16. 文本分类TextCNN
  17. leetcode988
  18. [T-ARA][O My God]
  19. Nginx安装 默认虚拟主机 Nginx用户认证 Nginx域名重定向
  20. 8 -- 深入使用Spring -- 3...2 ResouceLoader 接口和 ResourceLoaderAware 接口

热门文章

  1. 解决前端跨域请求(SpringBoot)
  2. 洛谷P1002 过河卒 [2017年4月计划 动态规划15]
  3. python的工具pip进行安装时出现 No module named &#39;pip&#39;
  4. 【python爬虫】加密代理IP的使用与设置一套session请求头
  5. PYTHON网络爬虫与信息提取[正则表达式的使用](单元七)
  6. JavaScript--预解析在IE存在的问题
  7. ubuntu下C操作Mysql数据库第一步
  8. CSS浏览器兼容解决方案
  9. Directx11教程(6) 画一个简单的三角形(2)
  10. jQuery打飞机游戏