根据它们的 id 是奇数还是偶数进行交替 React
我正在将 React 用于网页。在组件中,左侧有一个 div,右侧有一个图像。
我希望它们根据它们的 id 是奇数还是偶数来交替。
function Item({ title, text, link, img,id }) {
return (
<div >
<div>
<h3>
{title}
</h3>
<p>{text}</p>
<a href={link}>
text
</a>
</div>
<img src={img} alt={title} />
</div>
);
}
I am using React for a web page. In a component, I have a div on the left and an image on the right.
I would like them to alternate depending on whether their id is odd or even.
function Item({ title, text, link, img,id }) {
return (
<div >
<div>
<h3>
{title}
</h3>
<p>{text}</p>
<a href={link}>
text
</a>
</div>
<img src={img} alt={title} />
</div>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 div 转换为组件,并按照您想要的顺序有条件地渲染它们:
Turn the div into a component and conditionally render them in the order you want: