现在就开始学习React,在映射到Planet数组后,似乎无法让Planet组件显示服装。即使当我从Planet组件中进行console.log记录时,它也会显示为undefined。显然,我在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')
);
答案 0 :(得分:1)
您的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.children
的形式提供给Container
组件。
您可以在此处了解更多信息:https://reactjs.org/docs/composition-vs-inheritance.html
还有很多其他好的文章都在谈论React的children
道具-只是Google React props.children
答案 1 :(得分:0)
也许您在传递数据时丢失了某些东西。此外,您的代码是多余的,并且可能在某些时候崩溃。通过必要的安全检查来进行适当的结构重组。此外,Container
应该接收行星列表(数组),而Planet
组件应该仅接收行星列表中的一项,而不是整个数组。使用这段代码。我已经在测试后发布了它。希望您能从此代码中学到一些知识。
const App = props => {
// destructuring data with extra safety checks to prevent code crashes
const { planets = [] } = props || {};
return <Container planets={planets} />;
};
const Container = props => {
// adding extra guards to prevent code crashes
const { planets = [] } = props || {};
return (
<div className="container">
{planets && planets.map(planet => <Planet planet={planet} />)}
</div>
);
};
const Planet = props => {
const { planet = {} } = props || {};
return (
<div className="card">
<div>
<img src={planet.url} alt={planet.name} />
</div>
<h2>{planet.name}</h2>
<p>{planet.desc}</p>
<h3>Planet Profile</h3>
<ul>
<li>
<strong>Diameter:</strong>
{planet.diameter}
</li>
<li>
<strong>Moons:</strong> {planet.moons}
</li>
</ul>
</div>
);
};
ReactDOM.render(<App planets={planets} />, document.getElementById("root"));