使用 Instagram 提要创建幻灯片

Creating a slideshow with an instagram feed

我正在尝试创建一个显示 Instagram 源幻灯片的页面,使用库 instafeed.js and react-slick. A lot of sources writes about the ease of implementing a carousel of fotos from a feed using the jquery slick library 和 instafeed.js 喜欢这样:

let imgFeed = new Instafeed({
  get: 'user',
  userId: '',
  clientId: '',
  accessToken: '',
  resolution: 'standard_resolution',
  template: '<img src="{{image}}" />',
  sortBy: 'most-recent',
  limit: 8,
  links: false,
  after: function () {
    $('#instafeed').slick({
      infinite: true,
      slidesToShow: 4,
      slidesToScroll: 4
    });
  }
})

您只需在 after() 函数中添加灵活的功能。

查看 slick 库 react 版本的文档时,动态内容应该这样做:

import React, { Component } from 'react'
import Slider from 'react-slick'

export default class DynamicSlides extends Component {
  constructor(props) {
    super(props)
    this.state = {
      slides: [1, 2, 3, 4, 5, 6]
    }
    this.click = this.click.bind(this)
  }
  click() {
    const {slides}  = this.state
    this.setState({
      slides: slides.length === 6 ? [1, 2, 3, 4, 5, 6, 7, 8, 9] : [1, 2, 3, 4, 5, 6]
    })
  }
  render() {
    const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 3,
      slidesToScroll: 3
    };
    return (
      <div>
        <h2>Dynamic slides</h2>
        <button className='button' onClick={this.click}>Click to change slide count</button>
        <Slider {...settings}>
          {this.state.slides.map(function (slide) {
            return <div key={slide}><h3>{slide}</h3></div>
          })}
        </Slider>
      </div>
    );
  }
}

很多消息来源都在谈论使用这 2 个库制作 instagram-feed-carousel 是多么容易,但没有显示任何解决方案,所以我有点迷失了如何使它工作,因为我不知道如何将我的 instagram-feed 从 react-slick 动态添加到滑块组件。

我不熟悉 instafeed.js,但如果您可以从 Instagram 的 API 获取原始数据,这应该适合您:

import React from 'react'
import Slider from 'react-slick'

export default class InstagramCarousel extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      instagramData: this.props.instagramData
    }
  }
  render() {
    const settings = {

    }

    return (
      <Slider {...settings}>
        {this.state.instagramData.map((slide, index) => {
          return (
            <div key={index}>
              <a href={slide.link} target="_blank">
                <img src={slide.images.thumbnail.url} />
              </a>
            </div>
          )
        })}
      </Slider>
    )
  }
}

如果您有访问令牌,您可以获得 20 张最近的照片,如下所示:

import request from 'request'

let url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token='
request.get(url + accessToken, function (err, res, body) {
            this.setState({ ...this.state, instagramData: JSON.parse(body).data })
        })

并像这样调用组件:

<InstagramCarousel instagramData={this.state.instagramData} />