react双组件传值和传参
Posted l8l8
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于react双组件传值和传参的主要内容,如果未能解决你的问题,请参考以下文章