【发布时间】:2020-03-26 23:51:16
【问题描述】:
我是新来的反应。我想循环图像列表(Json)并在屏幕上显示这些图像。所以我想用数组创建一个道具。但我不知道如何将 Array 作为道具传递?你能帮忙看看吗?
var images = [
"./resources/bgdefault.jpg",
"./resources/bg1.jpg",
"./resources/bg2.jpg",
"./resources/bg3.jpg",
"./resources/bg4.jpg",
"./resources/bg5.jpg",
"./resources/bg6.jpg",
"./resources/bg7.jpg",
"./resources/bg8.jpg"
];
type State = {
index: number;
};
type Props = {
backgroundDuration: number;
imagePath: Array<string>;
};
export class BackgroundImage extends React.Component<Props, State> {
timerID: number;
constructor(props: Props) {
super(props);
this.state = {
index: -1
};
this.timerID=0;
}
.....
render() {
const path:string = images[this.state.index];
return (
<div
style={{
width: "100%",
height: "100%",
backgroundSize: "cover",
backgroundImage: `url(${path})`
}}
></div>
);
}
}
【问题讨论】:
-
请参考这个它已经被asnwered - stackoverflow.com/questions/31883601/…
-
这能回答你的问题吗? passings array as props in reactjs
标签: javascript reactjs typescript frontend