gpt4 book ai didi

javascript - 如何在 React 中呈现多个元素的列表?

转载 作者:行者123 更新时间:2023-12-01 02:21:56 25 4
gpt4 key购买 nike

寻找渲染解决方案const days = ["Day 1", "Day 2", "Day 3", "Day 4", "Day 5"];<div className="segment"> 。是否可以为输入标签元素制作渲染列表。

演示:https://codesandbox.io/s/536wmpj3mp

export class Segment extends React.Component {
render() {
const days = ["Day 1", "Day 2", "Day 3", "Day 4", "Day 5"];
return (
<div className="segment">
<input type="radio" id="day-1" name="day" value="Tay 1" />
<label className="segment-button" htmlFor="day-1">
day 1
</label>
<input type="radio" id="day-2" name="day" value="Tay 2" />
<label className="segment-button" htmlFor="day-2">
day 2
</label>
<input type="radio" id="day-3" name="day" value="Tay 3" />
<label className="segment-button" htmlFor="day-3">
day 3
</label>
<input type="radio" id="day-4" name="day" value="Tay 4" />
<label className="segment-button" htmlFor="day-4">
day 4
</label>
<input type="radio" id="day-5" name="day" value="Tay 5" />
<label className="segment-button" htmlFor="day-5">
day 5
</label>
</div>
);
}
}

最佳答案

您可以映射元素,例如:

import React from "react";

export class Segment extends React.Component {
render() {
const days = ["Day 1", "Day 2", "Day 3", "Day 4", "Day 5"];

return (
<div className="segment">
{
days.map((d, i) => {
return(
<div>
<input type="radio" id={`day-${i + 1}`} name="day" value={`Tay ${i + 1}`} />
<label className="segment-button" htmlFor={`day-${i}`}>{d}</label>
</div>
)
})
}
</div>
);
}
}

关于javascript - 如何在 React 中呈现多个元素的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49139313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com