【发布时间】:2019-04-15 19:55:51
【问题描述】:
我想了解为什么 react 会这样。
这行得通
class Feed extends React.Component {
constructor(props) {
super(props);
}
render() {
const posts = [{ id: 1, title: 'post-1' }, { id: 2, title: 'post-2' }];
return (
<>
{posts.map(post => (
<Post key={post.id} title={post.title} />
))}
</>
但这不是
class Feed extends React.Component {
constructor(props) {
super(props);
}
render() {
const posts = [{ id: 1, title: 'post-1' }, { id: 2, title: 'post-2' }];
return (
<>
{posts.map(post => {
// changes are here
if (post.id < 2) {
<Post key={post.id} title={post.title} />;
}
})}
</>
它只是返回空白。没有错误。
react 没有渲染这个的原因是什么?只渲染post-1 的最佳方法是什么?
【问题讨论】:
标签: reactjs