[英]How to loop items in child component from an object on the parent component in React JS
I have a view that shows project items. 我有一个显示项目项的视图。 Project information is in a data object in Projects (parent) component.
项目信息位于项目(父级)组件的数据对象中。
Parent component: 父组件:
import React from 'react';
import './projects.css';
import { Project } from '../projects/project/Project';
export class Projects extends React.Component {
constructor(props) {
super(props);
var projects = [
{
name: "Project 01",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea. A paragraph consists of one or more sentences.",
img: "http://wenuka.com/img/back.jpg"
},
{
name: "Project 02",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea.",
img: "http://wenuka.com/img/back.jpg"
},
{
name: "Project 03",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing one or more sentences.",
img: "http://wenuka.com/img/back.jpg"
}
]
}
render() {
return (
<section className="projects bg-ash">
<Project/>
</section>
);
}
};
HTML code for a project item is in the Project (child) component as below. 项目项的HTML代码在Project(子项)组件中,如下所示。
Child component: 子组件:
import React from 'react';
import './project.css';
export class Project extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div className="container work-item">
<div className="row">
<div className="col-lg-5">
<img src="http://wingman.mediumra.re/assets/img/graphic-product-paydar.jpg"/>
</div>
<div className="col-lg-5 image-box">
<h5>Project Name</h5>
<p>To write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea. A paragraph consists of one or more sentences.</p>
</div>
</div>
</div>
);
}
};
I need to show each element as a project in data object using child component. 我需要使用子组件将每个元素显示为数据对象中的项目。
Parent: 上级:
import React from 'react';
import './projects.css';
import { Project } from '../projects/project/Project';
export class Projects extends React.Component {
constructor(props) {
super(props);
this.state = {
projects: [
{
name: "Project 01",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea. A paragraph consists of one or more sentences.",
img: "http://wenuka.com/img/back.jpg"
},
{
name: "Project 02",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea.",
img: "http://wenuka.com/img/back.jpg"
},
{
name: "Project 03",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing one or more sentences.",
img: "http://wenuka.com/img/back.jpg"
}
]
};
}
render() {
return (
<section className="projects bg-ash">
{this.state.projects.map(project => (
<Project key={project.name} project={project}/>
))}
</section>
);
}
};
Child: 儿童:
import React from 'react';
import './project.css';
export class Project extends React.Component {
constructor(props) {
super(props);
}
render() {
let project = this.props.project; // Use this in jsx
...
}
};
You need to pass your parent data as props 您需要将父数据作为道具传递
lets say your parent class is : 假设您的父班是:
import React from 'react';
import './projects.css';
import { Project } from '../projects/project/Project';
export class Projects extends React.Component {
constructor(props) {
super(props);
// use this.state to initiate your state, keep data in state you can use variable not recommended
this.state = {
data : [
{
name: "Project 01",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea. A paragraph consists of one or more sentences.",
img: "http://wenuka.com/img/back.jpg"
},
{
name: "Project 02",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing dealing with a particular point or idea.",
img: "http://wenuka.com/img/back.jpg"
},
{
name: "Project 03",
desc: "A paragraph, from the Greek paragraphos, to write beside or written beside, is a self-contained unit of a discourse in writing one or more sentences.",
img: "http://wenuka.com/img/back.jpg"
}
]
}
}
render() {
return (
<section className="projects bg-ash">
<Project data={this.state.data}/>
</section>
);
}
};
and your child component should be: 并且您的子组件应为:
import React from 'react';
import './project.css';
export class Project extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{this.props.data.map((item,i)=>
<div className="container work-item" key={i}>
<div className="row">
<div className="col-lg-5">
<img src={item.img}/>
</div>
<div className="col-lg-5 image-box">
<h5>{item.name}</h5>
<p>{item.desc}</p>
</div>
</div>
</div>
);
}
};
See the Live demo here : https://codesandbox.io/s/mqj6j0o2y9 , 观看现场演示: https : //codesandbox.io/s/mqj6j0o2y9 ,
Have a good day 祝你有美好的一天
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.