Lists and keys
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>
);
}
Lists and keys的更多相关文章
- 27-React Lists and Keys
Lists and Keys React支持以数组的形式来渲染多个组件,它会将你数组中的每个组件以列表的形式渲染开来. 当你使用数组的方式来渲染你的组件时,你需要给每个组件一个Key值,否则会出现一个 ...
- iOS各版本图标尺寸汇总
About Information Property List Files UILaunchImageFile UILaunchImageFile (String - iOS) specifies t ...
- apple 官方文档 Push Notification Programming
iOS Developer LibraryDeveloper Search Local and Push Notification Programming Guide PDF Table of Con ...
- pythonic-让python代码更高效
何为pythonic? pythonic如果翻译成中文的话就是很python.很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等. 我的理解为,很+名词表达了一种特殊和强调的意味.所以很 ...
- React文档翻译 (快速入门)
翻译自react的大部分文档,方便自己查阅. 目录 生命周期 实例化 存在期 销毁期 state Do Not Modify State Directly State Updates May Be A ...
- 非关系型数据库之Redis
一.Redis简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用 ...
- Redis入门指南之三(入门)
本节主要介绍Redis的5种数据类型,同时使用Python API来操作Redis,其中python版本为3.5, redis版本为4.0.2. redis-py 的API的使用可以分类为: (1)连 ...
- ios push Payload
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotifi ...
- Python中字典的key都可以是什么
作者:Inotime 来源:CSDN 原文:https://blog.csdn.net/lnotime/article/details/81192207 答:一个对象能不能作为字典的key,就取决于其 ...
随机推荐
- 计蒜客 Prefix Free Code(字典树+树状数组)
Consider n initial strings of lower case letters, where no initial string is a prefix of any other i ...
- pycharm最新激活码2017
最新的2017激活码 BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZW ...
- 【Codeforces Round #430 (Div. 2) B】Gleb And Pizza
[链接]点击打开链接 [题意] 在这里写题意 [题解] 根据圆心到原点的距离这个东西判断一下圆在不在那个环里面就好 [错的次数] 0 [反思] 在这了写反思 [代码] #include <cst ...
- web前端学习(二)html学习笔记部分(8)--服务器推送事件3
1.2.22 html5服务器推送事件 1.2.22.1 html5服务器推送事件介绍 服务器推送事件(Server-sent Events)是HTML5规范中的一个组成部分,可以用来从服务器端实 ...
- 洛谷 P2955 [USACO09OCT]奇数偶数Even? Odd?【字符串/易错】
题目描述 Bessie's cruel second grade teacher has assigned a list of N (1 <= N <= 100) positive int ...
- sqlserver 一些基本的语法
1. STR()函数 这是将数字转换为字符串的快捷函数,这个函数有3个参数:数值,总长度,和小数位 如: select str(123.4,8,4) = 123.4000 如长度不够,则左 ...
- python findall函数
- php怎么自动加载
在 PHP 代码的顶部你是不是经常看到这样的代码. require 'lionis.php'; require 'is.php'; require 'cool.php'; 如果只是引入几个 PHP 脚 ...
- 如何建一个Liferay 7的theme
首先附上原文链接Creating theme and Deploying in liferay 7 by using Eclipse 1.第一步:建一个Liferay module 项目,选择them ...
- C++:static类
static自我理解 static使得数据成员或者函数生命周期为整个文件所在程序的生命周期, 在C中还可以用它避免被其它文件使用为外部成员 static类 明确:类的静态数据成员它被所有类对象共享,但 ...