【问题标题】:Reactjs: how to add next/previous button with rotating image galleryReactjs:如何使用旋转图片库添加下一个/上一个按钮
【发布时间】:2020-07-24 17:12:58
【问题描述】:

我正在使用 AliceCarousel 创建一个图片库。 我在这里遇到的一个错误是在制作nextprevious 时,它总是返回到初始图像`。它不会在下一张幻灯片上停下来。

我在做什么错误和帮助或建议。请。

//代码

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


    【解决方案1】:

    我已经使用owlcarousel 解决了这个问题。
    请查看更多OwlCarousel 这里我过滤了数组并返回了响应。

    render() {
        const { services, loading} = this.props.resume;
        var checkImage = services.length === 0 ? [] : services.filter((item) => item.service_name === "Kitchen")
        return(
          <div>
    
                <OwlCarousel className="owl-theme" loop margin={10} nav>
                   {checkImage.map((item, i) => ( 
                    <div className="col-xs-12 item" key={item._id} data-id={item._id} >
                      <img className="service-gallery-images" src={item.service_image_url} alt=""/>
                    </div>
                ))}
                </OwlCarousel>
          </div>
        )
      }
    

    【讨论】:

    • 谢谢 .. 这就是我要找的。​​span>
    【解决方案2】:

    问题很可能是您必须在方法中使用 prevState:

    像这样:

     slideNext = () => this.setState(prevState => ({ currentIndex: prevState.currentIndex + 1 }));
    

    对你的两种方法都这样做。

    【讨论】:

    • 嗨@Talmacel 谢谢你的时间。但我在这里也遇到了同样的错误。
    • 有几件事可能是问题...例如,为什么要使用 null 初始化 currentIndex?并在这种状态下传播你的道具?我怀疑那是个好主意。但是没有完整的代码来解决它很难查明实际问题。
    • 我只是试图通过将其设置为 null 来进行游戏,因为这里 npmjs.com/package/react-alice-carousel items=[1,2,3] 所以 slideNext is working with currentIndex` +1 其中默认 currentIndex 设置为 0。跨度>
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多