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>
  • 相关阅读:
    记录nginx 搭建集群环境踏过的坑
    工作中常见问题
    自定义WebViewPage,实现Url.Action生成绝对地址
    上传阿里云样式文件不解析问题一例总结
    EF4.4 升级EF6.0问题总结
    记一次数据库迁移遇到问题
    javascript 中 !~ 什么意思
    jquery.mobile 中 collapsible-set collapsible listview 共同布局问题
    T-sql脚本规范
    EasyUI使用技巧总结
  • 原文地址:https://www.cnblogs.com/l8l8/p/9457804.html
Copyright © 2011-2022 走看看