【发布时间】:2020-07-24 17:12:58
【问题描述】:
我正在使用 AliceCarousel 创建一个图片库。
我在这里遇到的一个错误是在制作next 或previous 时,它总是返回到初始图像`。它不会在下一张幻灯片上停下来。
我在做什么错误和帮助或建议。请。
//代码
class LivingService extends React.Component {
constructor(props) {
super(props);
this.state = {
currentIndex: null,
responsive: { 1024: { items: 4 } },
services : this.props.resume,
...props,
ItemsServices:[]
};
this.galleryItems = this.galleryItems.bind(this);
}
static propTypes = {
getService: PropTypes.func.isRequired,
resume: PropTypes.object.isRequired,
auth: PropTypes.object.isRequired,
loading: PropTypes.object.isRequired
}
UNSAFE_componentWillReceiveProps(nextProps) {
if(nextProps.resume !== this.props.resume){
var services = this.props.resume.services;
this.setState({
ItemsServices: services
})
}
}
componentDidMount() {
this.props.getService();
}
onSlideChanged = (e) => this.setState({ currentIndex: e.brand })
slideNext = () => this.setState(prevState => ({ currentIndex: prevState.currentIndex + 1 }));
slidePrev = () => this.setState(prevState => ({ currentIndex: prevState.currentIndex - 1 }));
thumbItem = (brand, i) => <span onClick={() => this.slideTo(brand._id)}>* </span>
slideTo = (_id) => this.setState({ currentIndex: _id })
galleryItems = () => {
return this.state.ItemsServices.map((brand, i) => {
if(brand.service_name === "Office"){
return (
<div className="col-xs-12" key={brand._id} data-id={brand._id} ><img src={brand.service_image_url} /></div>
)
}
})
};
render() {
const { responsive, currentIndex } = this.state
const items = this.galleryItems();
return(
<div>
<Grid className ="col-12 service-living-gallery-grid" >
<div className="service-gallery-headline">
Living
</div>
<AliceCarousel
dotsDisabled={true}
buttonsDisabled={true}
items={items}
responsive={responsive}
slideToIndex={currentIndex}
onSlideChanged={this.onSlideChanged}
/>
<ul className="gallery-carousel">{this.state.ItemsServices.map(this.thumbItem)}</ul>
<a className="carousel-control-prev gallery-carousel-arrows-left" role="button" onClick={() => this.slidePrev()}>
<span aria-hidden="true" className="carousel-control-prev-icon"></span>
<span className="sr-only">Previous</span>
</a>
<a className="carousel-control-next gallery-carousel-arrows-right" role="button" onClick={() => this.slideNext()}>
<span aria-hidden="true" className="carousel-control-next-icon"></span>
<span className="sr-only">Next</span>
</a>
</Grid>
</div>
)
}
}
const mapStateToProps = (state) => ({
resume: state.resume,
isAuthenticated : state.auth.isAuthenticated,
auth: state.auth,
loading: state.apiCallsInProgress > 0
});
export default connect(mapStateToProps, {getService }) (LivingService);
//API 响应
[
{
"_id": "5f1b0792c4659b079861d946",
"service_name": "Office",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595606941/ntyo9bgdtuzetlspyt7a.jpg",
"date": "2020-07-24T16:08:50.587Z",
"__v": 0
},
{
"_id": "5f1b0783c4659b079861d945",
"service_name": "Office",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595606926/tylafnrmtrxifognldqc.jpg",
"date": "2020-07-24T16:08:35.740Z",
"__v": 0
},
{
"_id": "5f1b0777c4659b079861d944",
"service_name": "Office",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595606914/w6h1t4acvadsynu4ip63.png",
"date": "2020-07-24T16:08:23.399Z",
"__v": 0
},
{
"_id": "5f1b0769c4659b079861d943",
"service_name": "Office",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595606900/maellafcuecaq3wjyb3o.png",
"date": "2020-07-24T16:08:09.846Z",
"__v": 0
},
{
"_id": "5f1971da18ba2b04704d65c2",
"service_name": "Other",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595503076/nou0knjbtkujxwjktang.png",
"date": "2020-07-23T11:17:46.928Z",
"__v": 0
},
{
"_id": "5f1971b218ba2b04704d65c1",
"service_name": "Bedroom",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595503036/kfiteeilh4doytio6gs8.png",
"date": "2020-07-23T11:17:06.742Z",
"__v": 0
},
{
"_id": "5f196d8e3620fa4024f3ea52",
"service_name": "Office",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595501975/pkwbmn7cftdq8uovf9po.png",
"date": "2020-07-23T10:59:26.345Z",
"__v": 0
},
{
"_id": "5f196d4f268e7925fccfa920",
"service_name": "Living",
"service_image_url": "https://res.cloudinary.com/tammycloudinary/image/upload/v1595501912/w8hrijdtf0wy97dpwglv.png",
"date": "2020-07-23T10:58:23.179Z",
"__v": 0
}
]
//sliderUI
【问题讨论】:
标签: javascript reactjs image-gallery