在JSX中显示具有属性的对象列表。

huangapple go评论78阅读模式
英文:

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:

        &lt;div className=&quot;mainApp&quot;&gt;
            &lt;div className=&quot;pkgsList&quot;&gt;
                {pkgData.map((p) =&gt; 
                    &lt;h3 key={p.name}&gt;{p.name}&lt;/h3&gt;                    
                )}
                    
            &lt;/div&gt;
        &lt;/div&gt;
    )

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) =&gt; {
                       
})

So the resulting div code would be:

            &lt;div className=&quot;pkgList&quot;&gt;
                {pkgData.map((p) =&gt; 
                    &lt;h3 key={p.name}&gt;{p.name}&lt;/h3&gt;
                    {p.map((c) =&gt; {
                        
                    })
                )}
            &lt;/div&gt;

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:

&lt;div className=&quot;mainApp&quot;&gt;
    &lt;div className=&quot;pkgsList&quot;&gt;
        {pkgData.map((p) =&gt; (
            &lt;&gt; {/* &lt;- this is a fragment */}
                &lt;h3 key={p.name}&gt;{p.name}&lt;/h3&gt;
                {p.sources.map((s) =&gt; (
                    &lt;&gt;{/* render source here */}&lt;/&gt;
                ))}
            &lt;/&gt;
        ))}
    &lt;/div&gt;
&lt;/div&gt;

答案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) =&gt; {
    ...
    }
  )}

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

&lt;div className=&quot;mainApp&quot;&gt;
  &lt;div className=&quot;pkgsList&quot;&gt;
    {pkgData.map((p) =&gt; 
      &lt;h3 key={p.name}&gt;{p.name}&lt;/h3&gt;                    
    )}
  &lt;/div&gt;
&lt;/div&gt;

is equivalent to this

&lt;div className=&quot;mainApp&quot;&gt;
  &lt;div className=&quot;pkgsList&quot;&gt;
    {pkgData.map((p) =&gt; {
      return React.createElement(&quot;h3&quot;, {key: p.name}, p.name)
      }                    
    )}
  &lt;/div&gt;
&lt;/div&gt;

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,

&lt;div className=&quot;mainApp&quot;&gt;
  &lt;div className=&quot;pkgsList&quot;&gt;
    {pkgData.map((p) =&gt;
      &lt;&gt;
        &lt;h3 key={p.name}&gt;{p.name}&lt;/h3&gt;
        {p.sources.map((c) =&gt; 
          &lt;p&gt;{c.something}&lt;/p&gt;
        )} 
      &lt;/&gt;
    )}
  &lt;/div&gt;
&lt;/div&gt;

FYI: https://react.dev/reference/react/createElement#creating-an-element-without-jsx

huangapple
  • 本文由 发表于 2023年5月24日 21:23:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定