gpt4 book ai didi

javascript - React.js 从 js 数组中获取数据

转载 作者:行者123 更新时间:2023-11-30 09:44:05 25 4
gpt4 key购买 nike

我有以下代码:

import React from 'react';

import ProjectsData from './projects.js';

class SingleProject extends React.Component {
render () {
return (
<div className="info-project-wrapper">
<h2>{this.props.title}</h2>
<span className="show-for-large">{this.props.by}</span>
<ul className="project-links show-for-large">
<li>{this.props.links}</li>
</ul>
<div className="info-project">
<p>VIEW NOW</p>
</div>
</div>
)
}
}

class SingleProjectWrapper extends React.Component {
render () {
var projects = [];
this.props.projects.forEach(function(project, i){
projects.push(<SingleProject title={project.title}
by={project.by}
links={projects.links}
key={i} />);
});
return (
<div className="single-project project-4">
{projects}
</div>
)
}
}

class Projects extends React.Component {
render () {
return (
<section>
<SingleProjectWrapper projects={ProjectsData} />
</section>
);
}
}

export default Projects;

和“projectsData”来自:

var projects = [
{
title: 'title1',
by: 'dfs',
links: ['link-1', 'link-2', 'link-3']
},
{
title: 'title2',
by: 'sdfsd',
links: ['link-1', 'link-2', 'link-3']
},
{
title: 'title3',
by: 'sfsf',
links: ['link-1', 'link-2', 'link-3']
},
{
title: 'title4',
by: 'sdffd',
links: ['link-1', 'link-2', 'link-3']
}
];

export default projects;

<li>{this.props.links}</li> 外,大部分数据都正确显示.我只得到一个空的 <li></li>而不是每个“link-1、link-2 和 link-3”。

最佳答案

您需要遍历链接数组,React 不会对数组做任何花哨的事情。

所以代替;

<ul className="project-links show-for-large">
<li>{this.props.links}</li>
</ul>

你需要做的;

<ul className="project-links show-for-large">
{this.props.links.map(i => <li>i</li>)}
</ul>

关于javascript - React.js 从 js 数组中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39707701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com