react-id-swiper无法设置未定义的属性“ control”

时间:2019-03-13 12:38:33

标签: reactjs swiper

我遇到的问题是我想使用缩略图创建两个滑动器,使它们的幻灯片同步,并且控件不起作用,首先,我需要知道为什么这样的滑动器不起作用,或者还有其他方法可以实现此效果,例如幻灯片演示,您可以在其中使用.map并渲染从我的API发送来的图像,文本和其他内容。

  42 | }
  43 | 
  44 | thumbRef(ref) {
> 45 |   if (ref) this.setState({ thumbnailSwiper: ref.swiper });
     |           ^  
  46 | }
  47 | render() {
  48 |   const params = {  




import React, { Component } from "react";
import axios from "axios";
import "bootstrap/dist/css/bootstrap.min.css";
import Swiper from "react-id-swiper";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      apiUrl: API,
      original: [],
      thumbnail: [],
      gallerySwiper: null,
      thumbnailSwiper: null
    };
    this.galleryRef = this.galleryRef.bind(this);
    this.thumbRef = this.thumbRef.bind(this);
  }

  componentDidMount() {
    axios({
      url: (`${this.state.apiUrl}`),
      method: "post",
      data: JSON.stringify({ refresh: "true", editor_id: "13" })
    }).then(res => {
      this.setState({ original: res.data.output.items });
      this.setState({ thumbnail: res.data.output.items });
    });
  }

  componentWillUpdate(nextProps, nextState) {
    if (nextState.gallerySwiper && nextState.thumbnailSwiper) {
      const { gallerySwiper, thumbnailSwiper } = nextState;

      gallerySwiper.controller.control = thumbnailSwiper;
      thumbnailSwiper.controller.control = gallerySwiper;
    }
  }

  galleryRef(ref) {
    if (ref) this.setState({ gallerySwiper: ref.swiper });
  }

  thumbRef(ref) {
    if (ref) this.setState({ thumbnailSwiper: ref.swiper });
  }

  render() {
    const params = {
      containerClass: "action_itemsContainer",
      slidesPerView: 1,
      spaceBetween: 20,
      slidesPerGroup: 1,
      autoplay: true,
      loop: true,
      loopFillGroupWithBlank: false,
      rebuildOnUpdate: true,
      crossFade: true
    };
    const thumbnailParams = {
      containerClass: "action_itemsThumb",
      direction: "vertical",
      slidesPerView: 3,
      spaceBetween: 5,
      slidesPerGroup: 1,
      autoplay: true,
      loop: true,
      loopFillGroupWithBlank: false,
      rebuildOnUpdate: true
    };

    return (
      <React.Fragment>
        <div className="container">
          <div className="row">
            <div className="col">
              <Swiper {...params} ref={this.galleryRef}>
                {this.state.original.map((items, i) => {
                  return (
                    <div key={i.toString()}>
                      <img className="img_editors" src={items.thumb_url} />
                    </div>
                  );
                })}
              </Swiper>
            </div>
            <div className="col">
              <Swiper {...thumbnailParams} ref={this.thumbRef}>
                {this.state.thumbnail.map((items, i) => {
                  return (
                    <div key={i.toString()}>
                      <img className="thumb_editors" src={items.thumb_url} />
                    </div>
                  );
                })}
              </Swiper>
            </div>
          </div>
        </div>
      </React.Fragment>
    );
  }
}

0 个答案:

没有答案