2 回答
TA贡献1856条经验 获得超5个赞
非常简单,只需使用假数组多次渲染六边形组件即可。
<div className='container'>
<div className="child">
{
new Array(10).fill(0).map((item, i) => {
return <Hexa key={i} hexaColor={hexaColor}/>
})
}
</div>
</div>
如果您只需要重复六边形颜色,请像那样更新六边形组件。
function Hexa(props) {
return (
<div>
<div className="child_content"> <h1>Random Colors</h1>
<h2>Hexadecimal Colors</h2>
{
new Array(10).fill(0).map((item, i) => {
return (
<React.Fragment key={i}>
<div className="background_div" style={{ backgroundColor: props.hexaColor() }} >
<div className="hexa_center"> {props.hexaColor() } </div>
</div>
</React.Fragment>
)
})
}
</div>
</div>
)
}
function App() {
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length);
color += str[index];
console.log(color);
}
return '#' + color
}
return (
<div className='container'>
<div className="child">
<Hexa hexaColor={hexaColor}/>
</div>
</div>
)
}
TA贡献1795条经验 获得超7个赞
您应该重复您的 Hexa 组件。
我已经解决了这样的问题:
你的 App.js
import React from "react";
import Hexa from "./components/Hexa";
export default function App() {
const hexaColor = () => {
let str = "0123456789abcdef";
let color = "";
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length);
color += str[index];
}
return "#" + color;
};
const createManyHexaComp = (iteration) => {
let result;
for (let i = 0; i < iteration; i++) {
result = <>
{result}
<Hexa hexaColor={hexaColor} />
</>;
}
return result;
}
return (
<div className="container">
<div className="child">
<div className="child_content">
{" "}
<h1>Random Colors</h1>
<h2>Hexadecimal Colors</h2>
{createManyHexaComp(5)}
</div>
</div>
</div>
);
}
你的 Hexa 组件
import React from 'react';
export default function Hexa(props) {
return (
<div>
<div
className="background_div"
style={{ backgroundColor: props.hexaColor() }}
>
<div className="hexa_center"> {props.hexaColor()} </div>
</div>
</div>
);
};
添加回答
举报