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>
);
};
添加回答
舉報