gpt4 book ai didi

reactjs - 隐藏/显示 React-slick 箭头

转载 作者:行者123 更新时间:2023-12-03 13:50:35 24 4
gpt4 key购买 nike

我正在使用react-slick,并且我有自己的自定义箭头。该 slider 不是无限循环,我想隐藏循环开始和结束处的箭头。所以基本上在开始时 PrevArrow 应该被隐藏,最后 NextArrow 应该被隐藏。我试图根据状态变化设置隐藏类名称。然而,尽管状态正在正确更改,但类名并未更改。你知道这段代码有什么问题吗?以及如何让它发挥作用?

下面是我对 Slider 和渲染 Slider 的组件的设置。

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Slider from 'react-slick';
import Arrow from '../slider/Arrow';


export default class CityCarousel extends Component {
constructor(props) {
super(props);

this.state = {
displayLeftArrow: true,
displayRightArrow: true
};


this.slidesToShow = 5;
this.sliderSetting = {
dots: false,
arrows: true,
infinite: false,
initialSlide: 0,
slidesToShow: this.slidesToShow,
slidesToScroll: 1,
speed: 500,
rows: 0,
nextArrow: <Arrow
styleClassName={`city-carousel__right ${
this.state.displayRightArrow ? '' : 'city-carousel__right--hide'
}`}
direction="right"
clickHandler={this.clickHandler}
/>,
prevArrow: <Arrow
styleClassName={`city-carousel__left ${
this.state.displayLeftArrow ? '' : 'city-carousel__left--hide'
}`}
direction="left"
clickHandler={this.clickHandler}
/>,
afterChange: currentSlide => this.setArrowDisplay(currentSlide)
};
}

clickHandler = direction => {
if (direction === 'left') {
this.slider.slickPrev();
} else if (direction === 'right') {
this.slider.slickNext();
}
};

setArrowDisplay = currentSlide => {
const { cityList } = this.props;
const displayLeftArrow = currentSlide !== 0;
const displayRightArrow = currentSlide !== cityList.length - this.slidesToShow;

this.setState({ displayRightArrow, displayLeftArrow });
};

render() {
const { cityList, tours } = this.props;
return (
<div>
<div className="city-selection">
<Slider
ref={c => this.slider = c }
{...this.sliderSetting}
>
{cityList.length > 0 ? this.renderCityList() : null}
</Slider>
</div>
</div>
);
}
}

这也是 Arrow 组件的代码

import React from 'react';
import PropTypes from 'prop-types';

const Arrow = ({ styleClassName, direction, clickHandler }) => {

return(
<div
className={`slick-arrow--${direction} slider-arrow--${direction} ${styleClassName}`}
onClick={e => clickHandler(direction)}
/>
)};

Arrow.propTypes = {
styleClassName: PropTypes.string,
direction: PropTypes.string,
clickHandler: PropTypes.func
};

export default Arrow;

最佳答案

看来react-slick没有重新渲染<Arrow />使用新的 Prop ,并且始终使用第一个初始化的 Prop 设置。

我认为解决方案是获取 <Arrow />像这样的 slider :

    <div className="city-selection">
<Arrow
styleClassName={`city-carousel__right ${
this.state.displayRightArrow ? '' : 'city-carousel__right--hide'
}`}
direction="right"
clickHandler={this.clickHandler}
/>
<Arrow
styleClassName={`city-carousel__left ${
this.state.displayLeftArrow ? '' : 'city-carousel__left--hide'
}`}
direction="left"
clickHandler={this.clickHandler}
/>
<Slider
{/* list of items*/}
</Slider>
</div>

你可以在 here 中看到它是如何工作的

关于reactjs - 隐藏/显示 React-slick 箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48257176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com