React 组件不会映射数组

杰西

现在刚开始学习 React,我似乎无法让 Planet 组件在映射到 Planet 数组后显示属性。即使当我 console.log 来自 Planet 组件的某些内容时,它也会显示为未定义。显然我在我的 App 组件和我的 Planet 组件之间搞砸了一些东西,但我不知道是什么。

const planets = [
  {
    id: '1',
    name: 'Mercury',
    diameter: '3,031.67 mi',
    moons: 'none',
    desc: 'Mercury is the closest planet to the Sun. Due to its proximity, it\'s not easily seen except during twilight. For every two orbits of the Sun, Mercury completes three rotations about its axis. Up until 1965 it was thought that the same side of Mercury constantly faced the Sun.',
    url: 'img/mercury.jpg' 
  },
  {
    id: '2',
    name: 'Venus',
    diameter: '7,521 mi',
    moons: 'none',
    desc: 'Venus is the second planet from the Sun and is the second brightest object in the night sky after the Moon. Venus is the second largest terrestrial planet and is sometimes referred to as the Earth’s sister planet due the their similar size and mass.',
    url: 'img/venus.jpg' 
  },
  {
    id: '3',
    name: 'Earth',
    diameter: '7,917.5 mi',
    moons: '1',
    desc: 'Earth is the third planet from the Sun and is the largest of the terrestrial planets. The Earth is the only planet in our solar system not to be named after a Greek or Roman deity. The Earth was formed approximately 4.54 billion years ago and is the only known planet to support life.',
    url: 'img/earth.jpg' 
  },
  {
    id: '4',
    name: 'Mars',
    diameter: '4,212 mi',
    moons: '2',
    desc: 'The fourth planet from the Sun and the second smallest planet in the solar system. Mars is often described as the "Red Planet" due to its reddish appearance. It\'s a terrestrial planet with a thin atmosphere composed primarily of carbon dioxide.',
    url: 'img/mars.jpg'
  },
  {
    id: '5',
    name: 'Jupiter',
    diameter: '86,881.4 mi',
    moons: '79',
    desc: 'The planet Jupiter is the fifth planet out from the Sun, and is two and a half times more massive than all the other planets in the solar system combined. It is made primarily of gases and is therefore known as a "gas giant".',
    url: 'img/jupiter.jpg' 
  },
  {
    id: '6',
    name: 'Saturn',
    diameter: '72,367.4 mi',
    moons: '62',
    desc: 'Saturn is the sixth planet from the Sun and the most distant that can be seen with the naked eye. Saturn is the second largest planet and is best known for its fabulous ring system that was first observed in 1610 by the astronomer Galileo Galilei.',
    url: 'img/saturn.jpg'
  },
  {
    id: '7',
    name: 'Uranus',
    diameter: '31,518 mi',
    moons: '27',
    desc: 'Uranus is the seventh planet from the Sun. While being visible to the naked eye, it was not recognised as a planet due to its dimness and slow orbit. Uranus became the first planet discovered with the use of a telescope.',
    url: 'img/uranus.jpg' 
  },
  {
    id: '8',
    name: 'Neptune',
    diameter: '30,599 mi',
    moons: '14',
    desc: 'Neptune is the eighth planet from the Sun making it the most distant in the solar system. This gas giant planet may have formed much closer to the Sun in early solar system history before migrating to its present position.',
    url: 'img/neptune.jpg' 
  },
];


   const App = (props) => {
  return (
  <Container planets={props.planets} >

    {props.planets.map((planet) => {
    return ( <Planet
      name={planet.name}
      key={planet.id.toString()} 
      name={planet.name} 
      diameter={planet.diameter} 
      moons={planet.moons}
      desc={planet.desc}
      url={planet.url}

      />)
  })}

  </Container>
  );
  }

  const Planet = (props) => {
         console.log(props.url)
      return (
       <div className="card">
        <div>
          <img src={props.url} alt={props.name} />
        </div>
        <h2>{props.name}</h2>
        <p>{props.desc}</p>
        <h3>Planet Profile</h3>
        <ul>
          <li><strong>Diameter:</strong>{props.diameter}</li>
          <li><strong>Moons:</strong> {props.moons}</li>
        </ul>
      </div>
  );
  };


  const Container = (props) => {
  console.log(props.planets.length)
    return (
      <div className="container">
        <Planet planets={props.planets} 
      />
      </div>
  );
  };


  ReactDOM.render(
    <App planets={planets}  />,
    document.getElementById('root')
  );
Yurui Zhang

您的Container组件未呈现其children

const Container = props => {
  return (
    <div className="container">
      <Planet planets={props.planets} />
    </div>
  );
};

注意div标签之间,你只渲染了一个Planet组件。

您可能应该将其更改为:

const Container = props => {
  return (
    <div className="container">{props.children}</div>
  );
};

因此,在您的App组件中,您map现在可以使用该功能。

    <Container>
      {props.planets.map(planet => {
        return (
          <Planet
            name={planet.name}
            key={planet.id.toString()}
            name={planet.name}
            diameter={planet.diameter}
            moons={planet.moons}
            desc={planet.desc}
            url={planet.url}
          />
        );
      })}
    </Container>

查看Container标签之间的所有内容- 将提供props.childrenContainer组件。

你可以在这里了解更多信息:https : //reactjs.org/docs/composition-vs-inheritance.html

还有很多其他关于 Reactchildren道具的好文章- 只是谷歌React props.children

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从数组映射时,React子组件不会重新渲染

来自分类Dev

如何在React组件中使用数组映射JSON数据

来自分类Dev

React-映射要返回的组件数组

来自分类Dev

如何使用 React 组件处理递归数组映射?

来自分类Dev

数组属性更改时,React子组件不会重新渲染

来自分类Dev

onClick 函数不会触发映射的函数,然后在 React 组件中呈现 div

来自分类Dev

映射对象数组React

来自分类Dev

React setState不会更新组件

来自分类Dev

无法将React JS中的数组数据映射到子组件

来自分类Dev

在同一组件中映射和渲染两个不同的数组-React

来自分类Dev

通过数组映射并在 React 中渲染指定的组件

来自分类Dev

是否可以使用 ref 访问数组映射函数内的 React 组件?

来自分类Dev

如何使用defaultProps为React函数组件定义接口而不会引发丢失的类型错误?

来自分类Dev

React Redux-映射到数组,同时仅重新呈现持有更改对象的组件(在数组中)

来自分类Dev

映射数组React Typescript错误

来自分类Dev

如何使用React映射数组

来自分类Dev

React - 通过图像数组映射

来自分类Dev

无法在 React 中映射数组

来自分类Dev

React - 映射出组件返回错误

来自分类Dev

渲染从 Firebase Firestore 映射的 React 组件

来自分类Dev

创建React组件数组?

来自分类Dev

React - 搜索组件以过滤数组

来自分类Dev

onClick不会呈现新的react组件。

来自分类Dev

React js更改状态不会更新组件

来自分类Dev

React组件不会影响Rails视图

来自分类Dev

React Redux不会重置某些组件的存储

来自分类Dev

Mobx不会重新渲染React组件

来自分类Dev

React/Redux 组件不会重新渲染

来自分类Dev

React setState 不会立即更新组件视图

Related 相关文章

热门标签

归档