我认为,您可以使用 react-slick + react-image-lightbox。本地状态是保存当前图像路径的好地方;
import Slider from 'react-slick';
import Lightbox from 'react-image-lightbox';
class Slider extends React.Component{
constructor(props){
super(props);
this.state = {
images: ['src/image1.png', 'src/image2.png', 'src/image3.png'],
current: ''
}
}
getSliderSettings(){
return {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1
}
}
handleClickImage = (e, image) => {
e && e.preventDefault();
this.setState({
current: image
})
}
handleCloseModal = (e) => {
e && e.preventDefault();
this.setState({
current: ''
})
}
render(){
const settings = this.getSliderSettings();
const { images, current } = this.state;
return (
<div>
<Slider {...settings}>
{ images.map(image => (
<img src={image} onClick={ e => this.handleClickImage(e, image)} />
))}
</Slider>
{current &&
<Lightbox
mainSrc={current}
onCloseRequest={this.handleCloseModal}
/>
}
</div>
)
}
}