我有来自休息 api 的图像进入我的应用程序。然后将这些图像填充到react-multi-carousel中。我想做的是使这些图像可点击用于灯箱。我对 React 还很陌生。到目前为止,我的轮播滑块代码是:
滑块.js
import React, { Component } from 'react';
import '../slider/slider.css';
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
const responsive = {
superLargeDesktop: {
breakpoint: { max: 4000, min: 3000 },
items: 1,
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 1,
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 1,
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1,
},
};
class Slider extends Component {
_isMounted = false;
state = {
awsApiData: [],
loading: false,
}
componentDidMount() {
this._isMounted = true;
console.log('app mounted');
this.setState({ loading: true });
/*global fetch */
fetch('https://onelbip0e6.execute-api.eu-west-2.amazonaws.com/xxxxx')
.then(data => data.json())
.then(data => this.setState({ awsApiData: data[0], loading: false }, () => console.log(data)));
}
componentWillUnmount() {
this._isMounted = false;
}
render() {
return (
<div>
{this.state.loading
? <div className="text-center">Loading...</div>
:
<Carousel
additionalTransfrom={0}
showDots={false}
arrows={true}
autoPlaySpeed={3000}
autoPlay={false}
centerMode={false}
className="slider"
containerClass="container-with-dots"
dotListClass="dots"
draggable
focusOnSelect={false}
infinite
itemClass="carousel-top"
keyBoardControl
minimumTouchDrag={80}
renderButtonGroupOutside={false}
renderDotsOutside
responsive={responsive}>
{Object.values(this.state.awsApiData).map((post, indx) => {
return (
<div className="mt-5" key={indx}>
<img className="media-img card-img-top card-img-hero" src={post.image} alt="Alt text"></img>
</div>
);
})}
</Carousel>
}
</div>
);
}
}
export default Slider;