Html REACT JS-增量组件类名?
有一个问题,是否可以在React JS中增加类名 这样做:Html REACT JS-增量组件类名?,html,css,reactjs,increment,classname,Html,Css,Reactjs,Increment,Classname,有一个问题,是否可以在React JS中增加类名 这样做: /* in first file */ const rowCount = 68; class PhotosItem extends Component { constructor() { super(); this.list = Array(rowCount).fill().map((val, idx) => { return { value : 0 } });
/* in first file */
const rowCount = 68;
class PhotosItem extends Component {
constructor() {
super();
this.list = Array(rowCount).fill().map((val, idx) => {
return {
value : 0
}
});
}
render() {
return (
<>
{this.list.map(this.renderRow)}
</>
);
}
renderRow(item) {
return (
<article class="photo__item">
<PhotosFigure />
</article>
);
}
}
export default PhotosItem;
/*在第一个文件中*/
const rowCount=68;
类项目扩展组件{
构造函数(){
超级();
this.list=数组(rowCount).fill().map((val,idx)=>{
返回{
数值:0
}
});
}
render(){
返回(
{this.list.map(this.renderRow)}
);
}
渲染箭头(项目){
返回(
);
}
}
导出默认项目;
/*在其他文件中*/
类photoFigure扩展了React.Component{
渲染(){
返回(
);
}
}
导出默认图形;
我想要这样的渲染:
<article class="photo__item">
<div class="figure photo_1"></div>
</article>
<article class="photo__item">
<div class="figure photo_2"></div>
</article>
[...]
<article class="photo__item">
<div class="figure photo_68"></div>
</article>
[...]
你有我的答案吗?还是更好的方法
thks
(我是法国人,如果我在英语中犯了错误,我真的很抱歉)第二个参数是map函数数组的索引。因此,可以将renderRow函数更改为该函数,并将索引作为道具传递
renderRow(item, index) {
return (
<article class="photo__item">
<PhotosFigure index={index+1} />
</article>
);
}
类名是字符串。两个字符串的串联就是你要做的。一个字符串是名称,另一个是索引。
renderRow(item, index) {
return (
<article class="photo__item">
<PhotosFigure index={index+1} />
</article>
);
}
class PhotosFigure extends React.Component{
render (){
return (
<>
<div className={`figure photo_${this.props.index}`}></div>
</>
);
}
}