简体   繁体   English

如何在 JSX 代码中打印数组的所有元素(作为来自父状态的道具传递)?

[英]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

or how many numbers the array has. 或数组有多少个数字。

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.

相关问题 如何断开子道具与传入的父母的状态? - How to disconnect child props from Parent's state passed in? 如何从 function 返回一个数组并将其元素用作 JSX - How can I return an array from function and use it's elements as a JSX 我如何将孩子的状态提升给其父级,以便可以将其作为道具发回去? - How can I lift a child's state to its parent so I can send it back down as props? 如何将 JSX 映射逻辑与包含带有 props 的组件的映射数组分开? - How can I separate the JSX mapping logic from the mapped array containing components with props? 如何清除/重置 React (JSX) 中的数组内部状态 - How do I clear/reset an array inside state in React (JSX) 当道具也被传递时如何映射元素? - How can I map elements when props are also passed? 如何从数组引用JSX组件道具? - How to reference JSX component props from array? 如何将 props 传递给 JSX 引用? - How can I pass props to JSX reference? 如何在收到的道具的组件中的所有JSX元素上重置类名? - How do I reset class names on all JSX elements in a component on props received? 如何通过在 ReactJS 中将 props 从父组件传递到子组件来初始化状态? - How can i initialize state by passing props from parent component to child component in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM