【发布时间】:2021-12-16 23:11:51
【问题描述】:
我正在尝试为我的 for 循环中的“customStyle.top”分配一个随机值,但 customStyle.top/customStyle.Left 值不会随机变化。我的代码如下:
import React from "react";
const customStyle = {
position: "absolute",
top: "",
left: "",
zIndex: "initial",
color: "green"
};
// Splitting Letters
const SplitText = React.memo(({ str }) => {
return (
<div>
{str.split("").map((item, index) => {
var randLeft = Math.floor(Math.random() * 1000);
var randTop = Math.floor(Math.random() * 600);
let letter = item;
for (let i = 0; i < item; i++) {
customStyle.top = +randTop + "px";
customStyle.left = +randLeft + "px";
letter = letter + i;
}
console.log(customStyle.top);
return (
<div key={index} style={customStyle}>
{letter}
</div>
);
})}
</div>
);
});
export default SplitText;
链接到我的 CodeSandbox:Click.!
【问题讨论】:
标签: javascript css reactjs react-hooks