【发布时间】:2019-11-21 20:25:01
【问题描述】:
我是新手。我想创建一个使用随机数动态更改 3 张图像的函数。
示例:如果数字 > 5 = image1,如果数字 > 10 = image2,如果数字 > 15 = image3
random(min,max,x) {
min = Math.ceil(5);
max = Math.floor(20);
return x = Math.floor(Math.random() * (max - min)) + min;
}
colormachine = () => {
if(this.random(this.props.x > 8))
{
this.setState(state => ({color: green}));
};
}
render() {
console.log(this.random());
// console.log(this.colormachine());
({ color: green }) 是一个
const green = { backgroundImage: "url(" + tirelogogreen + ")";
【问题讨论】:
-
将你的代码贴到问题本身,代码图片不可读(更不用说不能复制用于测试目的)
-
当然,抱歉。修正了这个问题。 @PatrickEvans
-
state应该只包含数据。它不应包含样式和图像。 -
啊,好吧..我以为我可以那样做..现在变得更复杂了
标签: javascript reactjs state assign