react 中文文档案例五 (循环列表)
Posted lolita-web
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react 中文文档案例五 (循环列表)相关的知识,希望对你有一定的参考价值。
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‘) );
//key值最好选用id,没有稳定的id的时候选择index const todoItems = todos.map((todo) => <li key={todo.id/index}> {todo.text} </li> );
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‘) );
以上是关于react 中文文档案例五 (循环列表)的主要内容,如果未能解决你的问题,请参考以下文章