英文:
Display a list of objects with properties in JSX
问题
以下是您的React组件代码的翻译:
return (
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) =>
<h3 key={p.name}>{p.name}</h3>
{p.sources.map((s) => {
// 在这里添加用于描述'sources'中项的HTML / JSX
})}
)}
</div>
</div>
)
请注意,在React JSX中,您需要使用大括号 {}
包装内部的JSX元素或代码。您可以在{p.sources.map((s) => { /* 在这里添加代码 */ })}
部分添加描述'sources'中项的HTML / JSX代码。
英文:
I have the following code from my React component:
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) =>
<h3 key={p.name}>{p.name}</h3>
)}
</div>
</div>
)
In this case, pkgData
is an array of objects which try to describe a package in this scenario. Each object contains a string propery name
and an array property sources
for which every item is also an object. I'd like to print the items inside sources
(a couple properties for each item) underneath my h3
element as above, but I can't seem to figure out how to add more HTML / JSX on what I have above - any help would be appreciated.
I keep trying to call map for each p but keep running into an error whether I wrap it on ()
or just {}
e.g.
{p.map((s) => {
})
So the resulting div code would be:
<div className="pkgList">
{pkgData.map((p) =>
<h3 key={p.name}>{p.name}</h3>
{p.map((c) => {
})
)}
</div>
does not let my react app compile successfully.
答案1
得分: 2
你在 map 函数中只能返回一个 JSX 元素,就像在普通组件中一样。如果你想在 map 函数中渲染多个元素,可以用 React 片段来包裹代码,就像这样:
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) => (
<> {/* 这是一个片段 */}
<h3 key={p.name}>{p.name}</h3>
{p.sources.map((s) => (
<>{/* 在这里渲染源 */}</>
))}
</>
))}
</div>
</div>
英文:
You can only return 1 jsx element in map like in a normal component. If you want to render multiple elements in the map function you can wrap the the code in a react fragment like so:
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) => (
<> {/* <- this is a fragment */}
<h3 key={p.name}>{p.name}</h3>
{p.sources.map((s) => (
<>{/* render source here */}</>
))}
</>
))}
</div>
</div>
答案2
得分: 2
错误的映射
首先,你的pkgData.map
内部嵌套映射是错误的。
因为p
本身不是数组。
映射应该像这样,
{p.sources.map((c) => {
...
}
)}
错误的JSX
其次,就像@David在评论中所说,由于JSX的工作方式,你不能拥有多个顶级元素。
例如,以下代码
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) =>
<h3 key={p.name}>{p.name}</h3>
)}
</div>
</div>
等同于
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) => {
return React.createElement("h3", {key: p.name}, p.name)
}
)}
</div>
</div>
因此,如果你在顶级返回多个JSX元素,React.createElement无法工作。
因此,你应该在顶级使用一些标签来包裹这些内容,比如fragments(<></>)、div等。
例如,
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) =>
<>
<h3 key={p.name}>{p.name}</h3>
{p.sources.map((c) =>
<p>{c.something}</p>
)}
</>
)}
</div>
</div>
FYI: https://react.dev/reference/react/createElement#creating-an-element-without-jsx
英文:
Wrong map
First, your nested map inside of pkgData.map
is wrong.
Because p
itself isn't array.
The map should be like this,
{p.sources.map((c) => {
...
}
)}
Wrong JSX
Second, like @David said in the comment, you can't have more than one top-level elements because of the way JSX works under the hood.
For example, the code
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) =>
<h3 key={p.name}>{p.name}</h3>
)}
</div>
</div>
is equivalent to this
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) => {
return React.createElement("h3", {key: p.name}, p.name)
}
)}
</div>
</div>
So if you return more than one JSX elements at the top level, React.createElement cannot work.
Therefore you should wrap the things up on the top level with some tags like fragments(<>), div, etc.
For example,
<div className="mainApp">
<div className="pkgsList">
{pkgData.map((p) =>
<>
<h3 key={p.name}>{p.name}</h3>
{p.sources.map((c) =>
<p>{c.something}</p>
)}
</>
)}
</div>
</div>
FYI: https://react.dev/reference/react/createElement#creating-an-element-without-jsx
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论