[英]How can I print all elements of an array (passed as props from the parent's state) inside JSX code?
I am new to React and I am trying to navigate the JSX syntax, combined with state/ props and pure Javascript.我是 React 新手,我正在尝试导航 JSX 语法,结合状态/道具和纯 Javascript。
I am passing props from the parent component (App) to the child component (Character).我将道具从父组件(App)传递给子组件(Character)。 One of the props is an array of numbers which I am trying to print to the page, inside a paragraph, when the child component gets rendered.
其中一个道具是一个数字数组,当子组件被渲染时,我试图在段落内打印到页面上。 I figured it out how to print all other props (which are either strings or numbers) but I can't find a solution for the seasons array.
我想出了如何打印所有其他道具(字符串或数字),但我找不到 seasons 数组的解决方案。 I can print all the elements of the array to the console using a forEach loop but I have no idea how to integrate the result into JSX.
我可以使用 forEach 循环将数组的所有元素打印到控制台,但我不知道如何将结果集成到 JSX 中。 I would like for the last paragraph of the child component to read
我想阅读子组件的最后一段
Seasons: 1, 2, 3季节:1、2、3
EDIT: I would like for each printed season to be a separate element (a link for example) and not to transform the array into a single string which reads "1, 2, 3" (like when using join(,).编辑:我希望每个打印的季节都是一个单独的元素(例如链接),而不是将数组转换为读取“1、2、3”的单个字符串(就像使用 join(,) 时一样。
Here is my code for the App component:这是我的 App 组件代码:
import React from 'react';
const Characters = (props) => {
// we use destructuring by storing the array of objects into the characters const
const { theCharacters } = props;
const charactersList = theCharacters.map(character => {
return (
<div className="character" key={character.id}>
<p>On screen name: {character.screenName}</p>
<p>Short description: {character.description}</p>
<p>Real name: {character.realName}</p>
<p>Apparitions: {
character.seasons.forEach(season => {
return season; //this here doesn't do anything
//console.log(season);
})}</p>
</div>
)
})
return (
<div className="character_list">
{charactersList}
</div>
)
}
export default Characters;
And this is the code for the Character component:这是 Character 组件的代码:
import React from 'react'; const Characters = (props) => { // we use destructuring by storing the array of objects into the characters const const { theCharacters } = props; const charactersList = theCharacters.map(character => { return ( <div className="character" key={character.id}> <p>On screen name: {character.screenName}</p> <p>Short description: {character.description}</p> <p>Real name: {character.realName}</p> <p>Apparitions: { character.seasons.forEach(season => { return season; //this here doesn't do anything //console.log(season); })}</p> </div> ) }) return ( <div className="character_list"> {charactersList} </div> ) } export default Characters;
Thank you!谢谢!
character.seasons.join(',')
it takes array elements and returns string, comma is a separator character.seasons.join(',')
它接受数组元素并返回字符串,逗号是分隔符
forEach
doesn't return anything. forEach
不返回任何内容。 You should use map
.您应该使用
map
。
{
character.seasons.map(season => <p>{season}</p>)
}
If you render an array of JSX elements, they'll render just fine;如果你渲染一组 JSX 元素,它们会渲染得很好; so you can just map the array of values to an array of JSX elements.
因此,您可以将 map 值数组转换为 JSX 元素数组。 Note that in these cases, it's important for each element to have a unique key that cannot change for that element, even if it moves positions in the array, to help React render them properly.
请注意,在这些情况下,重要的是每个元素都有一个唯一的键,即使它移动了数组中的位置,也不能更改该元素的唯一键,以帮助 React 正确渲染它们。 Luckily, you already have an ID for each character you can use:)
幸运的是,您已经为每个可以使用的角色拥有了一个 ID:)
So in your case, you can do something like this:因此,在您的情况下,您可以执行以下操作:
return (
<div className="character_list">
{charactersList.map(character => (
<span key={character.id}>{character.screenName}</span>
))}
</div>
)
And of course you can render more than just the screenName however you want in there.当然,您可以在其中渲染更多的屏幕名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.