zoukankan      html  css  js  c++  java
  • react双组件传值和传参

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script src="../node_modules/react/umd/react.development.js"></script>
        <script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
        <script src="../node_modules/babel-standalone/babel.js"></script>
    </head>
    <body>
        <div id="app"></div>
        <script src="./db.js"></script>
        <script type="text/babel">
        class Item extends React.Component{
            constructor(props){
                super(props);
            };
            render(){
                let {it}=this.props;
                return(
                <div>
                    {it.map((item,index)=>{//遍历拿到key值
                        return <div key={item.id}>
                                {Object.keys(item)}: //拿到数组
                                {Object.keys(item).map((v,ind)=>{
                                    return <div key={ind}> 
                                             {item[v]} //遍历进行解析内置的json内容
                                            </div>
                                })}  
                            </div>
                    })}
                </div>
                )
            }
        }
        class List extends React.Component{
            constructor(props){
                super(props);
            };
           render(){
             let {users,companies} = this.props;
             let arr =  Object.keys(this.props); //拿到json的key值数组
             return (
               <div>
                    { Object.keys(this.props)} //遍历传过来的json数据
                    {arr.map((item,ind)=>{   //遍历进行解析 拿到数组
                        return <Item key={ind} it={this.props[item]}/>
                    })}
                </div>
             )
           }
        }
        class App extends React.Component{
            constructor(props){
                super(props);
                this.state = {
                    data
                }
           };
            render(){
                return (
                <div>
                  <List users={data.users}/> //将数组分组传过去
                  <List companies={data.companies}/>
                </div>
                )
            }
        }
            ReactDOM.render(
                <App/>,
                document.getElementById('app')
            )
        </script>
    </body>
    </html>
  • 相关阅读:
    3月4号—3月20号的计划
    Codeforces Round #344 (Div. 2) D. Messenger kmp水题
    Codeforces Round #344 (Div. 2) C. Report 水题
    整数三分(模板)
    Codeforces Round #344 (Div. 2) E. Product Sum 三分
    hdu3276 Graph and Queries 离线+treap
    bzoj1588: [HNOI2002]营业额统计 treap
    hdu5002 tree LCT
    bzoj2594 [Wc2006]水管局长数据加强版 离线+LCT维护边权
    bzoj2002 弹飞绵羊 LCT
  • 原文地址:https://www.cnblogs.com/l8l8/p/9457804.html
Copyright © 2011-2022 走看看