-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo8_列表key.html
76 lines (70 loc) · 2.15 KB
/
demo8_列表key.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../build/react.dev.js"></script>
<script src="../build/react-dom.dev.js"></script>
<script src="../build/brower.min.js"></script>
<title>Demo-8</title>
</head>
<body>
<div id="demo-8"></div>
<script type="text/babel">
/*
* key会传递消息给react,但不会传消息给组件,如果组件中要用到key的值,可用其他
* 显示的属性传值,例如id;
*/
function CityList(props) {
const citys = props.citys;
const cityItems = citys.map((item) => <li key={ item.key }>{ item.value }</li>);
return (
<ul>{ cityItems }</ul>
);
}
function Blog(props) {
const slidbar = (
<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>
{ slidbar }
<hr />
{ content }
</div>
)
}
const citys = [
{key: "guangzhou", value: "广州"},
{key: "shenzhen", value: "深圳"},
{key: "beijing", value: "北京"},
{key: "shanghai", value: "上海"}
];
const posts = [
{id: 1, title: "Hello World", content: "Welcome to learning React!"},
{id: 2, title: "Installation", content: "You can install React from npm."}
];
ReactDOM.render(
<div>
<CityList citys={ citys } />
<hr />
<Blog posts={ posts } />
<hr />
</div>,
document.getElementById("demo-8")
)
</script>
</body>
</html>