1. 程式人生 > >react 中文文檔案例五 (循環列表)

react 中文文檔案例五 (循環列表)

you content .get text string key值 render 中文 number

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 中文文檔案例五 (循環列表)