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,就取决于其 ...
随机推荐
- iTerm2+oh-my-zsh配色
效果图,很帅气有木有 一.首先安装iTem2 安装好后的截图如下: 安装好后的截图 二.安装oh-my-zsh. sh -c "$(curl -fsSL https://raw.gi ...
- golang之结构体
Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. Go语言通过用自定义的方式形成新的类型,结构体是类型中带有成员的复合类型. Go 语言中的类型可以被实例化,使用new或&a ...
- JZOJ 平衡的子集
Description 夏令营有N个人,每个人的力气为M(i).请大家从这N个人中选出若干人,如果这些人可以分成两组且两组力气之和完全相等,则称为一个合法的选法,问有多少种合法的选法? Input 第 ...
- HDU 1724 自适应辛普森法
//很裸的积分题,直接上模板 #include<stdio.h> #include<math.h> int aa, bb; //函数 double F(double x){ - ...
- arcgis信息窗口
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 产生冠军 HDU - 2094 (拓扑排序)
分析: 当有且只有一个节点入度为0时,该节点即为冠军,否则不能产生冠军.所以以下代码中只要入度大于0的无论是几都将其设置为1. #include <stdio.h> #include &l ...
- Codeforces Round #416 (Div. 2) A. Vladik and Courtesy【思维/模拟】
A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- adb安装apk包时提示:device unauthorized
adb install apk时提示device unauthorized,手机上还没出现usb是否允许调试的询问弹框: 解决方法: 1.cmd输入:adb kill-server,点击回车键 2.c ...
- iOS Status Bar变换
http://www.cocoachina.com/ios/20160718/17020.html 背景 iOS 中经常会有需要在某个界面改变状态栏颜色或者某个界面隐藏状态栏的需求.而改变状态栏颜色和 ...
- Kubernetes 调度器实现初探
Kubernetes 调度器 Kubernetes 是一个基于容器的分布式调度器,实现了自己的调度模块.在Kubernetes集群中,调度器作为一个独立模块通过pod运行.从几个方面介绍Kuberne ...