学习React,决定设计自己的自行车(carousel),在连接handler的过程中,遇到了一个难以理解的情况。当我们以混乱的顺序按下幻灯片更改按钮时,幻灯片会在一张,然后是两张,以此类推。告诉我可能是什么原因。我认为这很可能是 setState() 方法。
export default class HeroCarousel extends Component {
state = {
slides: [
{
caption: {
title: 'Slide one',
text: `Lorem ipsum dolor sit amet, malorum recteque reprehendunt ea vel.
Urbanitas adolescens vim te, per at tritani aperiri. Adhuc invenire convenire his ea.`,
},
bg: 'images/content/hero_carousel/slides/img.jpg',
},
{
caption: {
title: 'Slide two',
text: `Lorem ipsum dolor sit amet, malorum recteque reprehendunt ea vel.
Urbanitas adolescens vim te, per at tritani aperiri. Adhuc invenire convenire his ea.`,
},
bg: 'images/content/hero_carousel/slides/img1.jpg',
},
{
caption: {
title: 'Slide three',
text: `Lorem ipsum dolor sit amet, malorum recteque reprehendunt ea vel.
Urbanitas adolescens vim te, per at tritani aperiri. Adhuc invenire convenire his ea.`,
},
bg: 'images/content/hero_carousel/slides/img2.jpg',
},
{
caption: {
title: 'Slide four',
text: `Lorem ipsum dolor sit amet, malorum recteque reprehendunt ea vel.
Urbanitas adolescens vim te, per at tritani aperiri. Adhuc invenire convenire his ea.`,
},
bg: 'images/content/hero_carousel/slides/img3.jpg',
},
{
caption: {
title: 'Slide five',
text: `Lorem ipsum dolor sit amet, malorum recteque reprehendunt ea vel.
Urbanitas adolescens vim te, per at tritani aperiri. Adhuc invenire convenire his ea.`,
},
bg: 'images/content/hero_carousel/slides/img4.jpg',
},
{
caption: {
title: 'Slide six',
text: `Lorem ipsum dolor sit amet, malorum recteque reprehendunt ea vel.
Urbanitas adolescens vim te, per at tritani aperiri. Adhuc invenire convenire his ea.`,
},
bg: 'images/content/hero_carousel/slides/img5.jpg',
},
],
breadcrumbs: [
{
previewImage: 'images/content/hero_carousel/breadcrumbs/img.jpg',
ariaLabel: 'Example breadcrumb',
},
{
previewImage: 'images/content/hero_carousel/breadcrumbs/img1.jpg',
ariaLabel: 'Example breadcrumb',
},
{
previewImage: 'images/content/hero_carousel/breadcrumbs/img2.jpg',
ariaLabel: 'Example breadcrumb',
},
{
previewImage: 'images/content/hero_carousel/breadcrumbs/img3.jpg',
ariaLabel: 'Example breadcrumb',
},
{
previewImage: 'images/content/hero_carousel/breadcrumbs/img4.jpg',
ariaLabel: 'Example breadcrumb',
},
{
previewImage: 'images/content/hero_carousel/breadcrumbs/img5.jpg',
ariaLabel: 'Example breadcrumb',
},
],
position: 0,
}
goToNextSlide = () => {
let {
position,
slides
} = this.state;
if (position === slides.length - 1) {
position = 0
}
++position
return this.setState({
position: position
})
}
goToPrevSlide = () => {
let {
position,
slides
} = this.state;
if (position === 0) {
position = slides.length - 1
}
--position
return this.setState({
position: position
})
}
render() {
return (
<div className="carousel" id="hero-carousel">
{/* Begin control next*/}
<HeroCarouselControlRight handler={this.goToNextSlide} />
{/* End control next transform ease-out 450ms */}
{/* Begin control previous*/}
<HeroCarouselControlLeft handler={this.goToPrevSlide} />
{/* End control previous */}
<div className="carousel__inner" style={{
transition: 'transform ease-out 450ms',
transform: `translateX(-${this.state.position}00%)`
}}>
{this.state.slides.map((slide, index) =>
<HeroCarouselSlide
title={slide.caption.title}
text={slide.caption.text}
key={index}
bg={slide.bg}
/>
)}
</div>
<ul className="carousel__thumbs">
{this.state.breadcrumbs.map((breadcrumb, index) =>
<HeroCarouselBreadCrumb
previewImage={breadcrumb.previewImage}
dataSlideTo={index}
ariaLabel={breadcrumb.ariaLabel}
handler={this.goToSlide}
key={index}
/>
)}
</ul>
</div>
)
}
}
问题是您正在“改变”状态。您需要返回一个新状态,而不是从一个状态增加/减少一个值。下面是一个正确实现的例子:
你可以在这里阅读更多。