使用动态内容对Swiper进行反应

使用动态内容对Swiper进行反应

本文介绍了使用动态内容对Swiper进行反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用react-id-swiper使用来自Instagram的图像制作轮播.响应后,Swiper组件似乎没有更新.我以为可以将setState放入componentWillMount中,但显然不行.当我在Chrome中打开检查器时,它开始工作了吗?

I'm making a carousel with images from Instagram using react-id-swiper. The Swiper component doesn't seem to be updating after the response. I thought that putting my setState inside componentWillMount would work but apparently not. When I open the inspector in Chrome it starts working?

import React, { Component } from 'react'
import Swiper from 'react-id-swiper'
import request from 'superagent'
import './index.css'

const swiperParams = {
  slidesPerView: 5,
  spaceBetween: 0,
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev'
  },
  pagination: {
    el: '.swiper-pagination',
    clickable: true
  },
}

class Carousel extends Component {
  constructor(props) {
    super(props)
    this.state = {
      photos: []
    }
  }

  componentWillMount() {
    this.fetchPhotos();
  }

  fetchPhotos() {
    request
      .get('https://api.instagram.com/v1/users/self/media/recent/?access_token=' + process.env.INSTAGRAM_ACCESS_TOKEN)
      .then((res) => {
        this.setState({
          photos: res.body.data
        })
      })
  }

  render() {
    return (
      <Swiper {...swiperParams}>
        {this.state.photos.map((photo, key) => {
          return (
            <div key={photo.id}>
              <img src={photo.images.standard_resolution.url} alt={photo.caption} />
            </div>
          )
        })}
      </Swiper>
    )
  }
}

export default Carousel

推荐答案

在您的对象中 swiperParamas 包括以下属性

In your object swiperParamas include the following propertie

rebuildOnUpdate: true

这篇关于使用动态内容对Swiper进行反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 21:05