渲染多样的组件
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<li>{number}</li>
);
ReactDOM.render(
<ul>{listItems}</ul>,
document.getElementById('root')
);
基础列表组件

render() {
const numbers = [1,6,8];
const listItems = numbers.map((number) =>
<li>{number}</li>
);
return (
<div>
<ul>{listItems}</ul>
</div>
);
}
Key的使用, 都挺符合ES6的对象的使用,也没什么好测试的了。定义一个对象,里面包含三个字段,选某一字段作为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>
);
}
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(
<Blog posts={posts} />,
document.getElementById('root')
);
网友评论