码迷,mamicode.com
首页 > 其他好文 > 详细

react 中文文档案例五 (循环列表)

时间:2019-02-02 14:24:20      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:const   rop   ati   document   number   world   render   index   doc   

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

标签:const   rop   ati   document   number   world   render   index   doc   

原文地址:https://www.cnblogs.com/Lolita-web/p/10348219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!