首页 > Web开发 > 详细

react.js 渲染一个列表的实例

时间:2017-09-19 13:11:58      阅读:298      评论:0      收藏:0      [点我收藏+]
import React,{Component} from ‘react‘;
import ReactDOM from ‘react-dom‘;

let users=[
    {id:1,name:‘老王1‘,age:31},
    {id:2,name:‘老王2‘,age:32},
    {id:3,name:‘老王3‘,age:33}
]
class User extends Component{
    render(){
        return(
            <tr>
                <td>{this.props.item.id}</td>
                <td>{this.props.item.name}</td>
                <td>{this.props.item.age}</td>
            </tr>
        )
    }
}
//在一个组件中,状态越少越好
class UserList extends Component{
    render(){
        return(
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>名字</th>
                    <th>年龄</th>
                </tr>
                </thead>
                <tbody>
                {
                    this.props.user.map((item,index)=>{
                    return (
                        <User item={item} key={index}></User>
                    )
                })
                }
                </tbody>
            </table>
        )
    }
}
ReactDOM.render(<UserList user={users}></UserList>,document.querySelector("#root"));

 

react.js 渲染一个列表的实例

原文:http://www.cnblogs.com/null11/p/7551195.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!