简体   繁体   English

维护多个反应光滑的索引

[英]Maintaining index of multiple react-slick

I am making a react project.我正在做一个反应项目。 I have a page where have to use multiple react-slick elements.我有一个页面,必须使用多个 react-slick 元素。 I want to maintain index of each slick element.我想维护每个光滑元素的索引。 Currently I am doing this目前我正在这样做

render() {
            var settings = {
            arrows: true,
            infinite: false,
            speed: 500,
            slidesToShow: 1,
            slidesToScroll: 1,
            afterChange: function(currentSlide){
            this.setState({
            currentSlide: currentSlide
        })
            console.log(currentSlide);
        }.bind(this)
        };
            return (

            <div className="main-wrapper">

            {/*carousel responsive1*/}

            {this.state.users.map((item,i)=>{
                return  <div className="main-wrapper-mobile">
                    <div className="carousel responsive1">
                        <div className="mask">
                            {/*slideset */}
                            <div className="slideset">
                                {/*slide*/}

                                <Slider {...settings}>
                                    {item.activities.map((item1,i)=>{
                                        return <div className="slide">
                                            <div className="content__item d-flex">

                                                <div className="content__photo">
                                                    <img className="content__img" src={item.image_url}/>
                                                </div>
                                                <div className="article">
                                                    <p className="article__text">{item.comment_text}</p>
                                                    <ul className="article__controls">
                                                        <li className="article__control-item"><a href="#">visual <i
                                                            className="fa fa-eye" aria-hidden="true"></i></a></li>
                                                        <li className="article__control-item"><a href="#">text <i
                                                            className="fas fa-comment-dots"></i></a></li>
                                                        <li className="article__control-item"><a href="#">edited <i
                                                            className="fas fa-edit"></i></a></li>
                                                    </ul>
                                                    <div className="article__icon">
                                                        <a href="#" className="article__icon--blue"> <i
                                                            className="fa fa-check-circle" aria-hidden="true"></i></a>
                                                        <a href="#" className="article__icon--gray"><i
                                                            className="fa fa-minus-circle" aria-hidden="true"></i></a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    })}

                                </Slider>
                            </div>
                        </div>

Brief explaination of above code:上面代码的简要说明:

I have an array of users.我有一系列用户。 Each user is assigned a slider.每个用户都分配了一个滑块。 That is why I am mapping the users array and rendering in each mapping.这就是我映射用户数组并在每个映射中呈现的原因。

Currently the "currentSlide" parameter in "settings" sets the state "currentSlide" to whatever currentSlide is.当前,“settings”中的“currentSlide”参数将状态“currentSlide”设置为 currentSlide 是什么。 The problem is the same state changes for all the sliders.问题是所有滑块的状态变化都相同。 There is no way to keep track of which slider is getting slide.无法跟踪哪个滑块正在滑动。 I should be able to change or maintain slider and it's corresponding index.我应该能够更改或维护滑块及其相应的索引。 How should I do it?我该怎么做?

You would have to dynamically create a new settings object per instance of react-slick, and attach an extra value (in this case I just added an extra string called "Slider1" or "Slider2", etc.) inside the callback function of afterChange.您必须为每个 react-slick 实例动态创建一个新的设置对象,并在 afterChange 的回调函数中附加一个额外的值(在这种情况下,我只是添加了一个名为“Slider1”或“Slider2”等的额外字符串) . Basically whenever the user changes the slides of on a particular react slider, the state under currentSlide would have an object that looks like this {indexSlider: "Slider1", indexOfCurrentSlide: 2 } .基本上,每当用户更改特定反应滑块上的幻灯片时, currentSlide下的state就会有一个看起来像这样的对象{indexSlider: "Slider1", indexOfCurrentSlide: 2 } Let me know if this clarifies what you are asking for.如果这澄清了您的要求,请告诉我。 I figured this is one way of distinguishing which Slider has been changed.我认为这是区分哪个 Slider 已更改的一种方法。

const MultipleSliders = () => {
  const [ currentSlide, setCurrentSlide ] = useState({})
  const generateSettings = (indexSlider) => {
    return {
      arrows: true,
      infinite: false,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1,
      afterChange: (indexOfCurrentSlide) => {
        setCurrentSlide({
          indexSlider,
          indexOfCurrentSlide
        })
      }
    }
  }
  const settings1 = generateSettings('Slider1');
  const settings2 = generateSettings('Slider2');
  const settings3 = generateSettings('Slider3');
  return (
    <React.Fragment>
      <Slider {...settings1}>
        {/* content goes here */}
      </Slider>
      <Slider {...settings2}>
        {/* content goes here */}
      </Slider>
      <Slider {...settings3}>
        {/* content goes here */}
      </Slider>
    </React.Fragment>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM