Css 带有React的随机背景图像

Css 带有React的随机背景图像,css,reactjs,background-image,Css,Reactjs,Background Image,我实际上正在开发一个React应用程序,目前正试图显示一个随机的背景图像。 我的想法是导入4张图片,将它们放入一个数组中,然后随机选择数组中的一项: 从“../assets/pictures/sky.jpg”导入skyPicture1 从“../assets/pictures/sky2.jpg”导入skyPicture2 从“../assets/pictures/sky3.jpg”导入skyPicture3 从“../assets/pictures/sky4.png”导入skyPicture4

我实际上正在开发一个React应用程序,目前正试图显示一个随机的背景图像。 我的想法是导入4张图片,将它们放入一个数组中,然后随机选择数组中的一项:

从“../assets/pictures/sky.jpg”导入skyPicture1
从“../assets/pictures/sky2.jpg”导入skyPicture2
从“../assets/pictures/sky3.jpg”导入skyPicture3
从“../assets/pictures/sky4.png”导入skyPicture4
类HomeForUnlocked扩展组件{
建造师(道具){
超级(道具)
此.state={
bgStyle:{
高度:“100%”,
背景位置:“中心”,
背景重复:“不重复”,
背景尺寸:“封面”,
}
}
}
组件willmount(){
const picturarray=[{skyPicture1}、{skyPicture2}、{skyPicture3}、{skyPicture4}];
const randomIndex=Math.floor(Math.random()*pictureArray.length);
常量selectedPicture=pictureArray[randomIndex];
这是我的国家({
bgStyle:{
背景图片:`url(${selectedPicture})`,
高度:“100%”,
背景位置:“中心”,
背景重复:“不重复”,
背景尺寸:“封面”,
}
})
}
render(){
返回(
); } } 导出默认HomeForUnlocked;
但根本没有显示任何图片。有人知道我做错了什么吗?
我没有任何线索。

如果将其作为对象,可以将其更改为常量pictureArray=[skyPicture1,skyPicture2,skyPicture3,skyPicture4]

如果将其设置为对象,是否可以将其更改为
const picturarray=[skyPicture1,skyPicture2,skyPicture3,skyPicture4]谢谢,现在可以了!你能检查我的答案是否正确吗?