【问题标题】:fetch Youtube video dynamically动态获取 Youtube 视频
【发布时间】:2017-10-11 17:08:41
【问题描述】:

我正在尝试使用 react.js 从您的管道频道获取视频

我尝试使用 PHP CURL,得到了正确的响应。下面的代码显示空白页。 请帮忙。

import $ from 'jquery'; 
import React, { Component } from 'react';;

class iLotIndex extends Component {

constructor(props) {
    super(props);

    this.state = {video: []};
  }

VideoList() {
    return $.getJSON('https://www.googleapis.com/youtube/v3/search?key=AIzaxxxxxxxxxxxxxxxxxxxxx&channelId=UCexxxxxxxxxxxxxxxx&part=snippet,id&order=date&maxResults=20')
      .then((data) => {
        this.setState({ video : data.results });
      });
  }


  render() {
    this.VideoList().then(function(res){
      this.state = {video: res};
    });
    return (
      <div id="layout-content" className="layout-content-wrapper">
        <div className="panel-list">
          {this.state.video.map((item, i) =>{
            return(
              <h1>{item.items}</h1>

            )
          })}
        </div>
      </div>
    )
  }
}

【问题讨论】:

  • 您的浏览器控制台有错误吗?
  • 你能提供小提琴吗?还是钢笔还是jsben.ch?
  • 不要混合使用 jQuery 和 React ;)
  • 我会推荐superagent。

标签: javascript reactjs youtube youtube-data-api


【解决方案1】:

this.VideoList() 调用移动到componentDidMount() 而不是render()

constructor(props) {
  super(props);

  this.state = {video: []};
  this.VideoList = this.VideoList.bind(this);
}

VideoList() {
  fetch('https://www.googleapis.com/youtube/v3/search?key=AIzaxxxxxxxxxxxxxxxxxxxxx&channelId=UCexxxxxxxxxxxxxxxx&part=snippet,id&order=date&maxResults=20')
    .then(resp => resp.json())
    .then((resp) => {
      console.log(resp);
      //this.setState({video: resp.results});
      this.setState({video: resp.items});
      console.log(this.state.video);
    });


  //$.getJSON('https://www.googleapis.com/youtube/v3/search?key=AIzaxxxxxxxxxxxxxxxxxxxxx&channelId=UCexxxxxxxxxxxxxxxx&part=snippet,id&order=date&maxResults=20')
    //.then((data) => {
      //this.setState({ video : data.results });
    //});
}

componentDidMount() {
  this.VideoList();
}

render() {

  return (
    <div id="layout-content" className="layout-content-wrapper">
      <div className="panel-list">
        {this.state.video.map((item, i) =>{
          console.log(item);
          return(
            <h1>{item.items}</h1>

          )
        })}
      </div>
    </div>
  )
}

请在此处发布一些错误,以防它还不起作用,谢谢!

【讨论】:

  • 我可以在控制台中看到来自 youtube api 的响应,但它仍然没有在网页上呈现任何 html。你能帮忙吗?
  • 现在可能没问题,您介意再试一次吗,谢谢!随意 console.log 为您的“视频”正确记录返回的数据和 setState。我看不到返回的 json 结构,所以希望这次它可以工作 ^^。我刚刚还修复了正确的“resp”变量名称,而不是之前的“数据”变量名称
  • 这是说 this.state.video 未定义
  • 请同时保留你的构造函数:constructor(props) { super(props); this.state = {视频:[]}; }
  • 我把 console.log(this.state.video) ;里面返回。它不会在控制台上打印任何日志
【解决方案2】:

我建议你用 axios 或者用 react fetch

axios.get('https://www.googleapis.com/youtube/v3/search?key=AIzaxxxxxxxxxxxxxxxxxxxxx&channelId=UCexxxxxxxxxxxxxxxx&part=snippet,id&order=date&maxResults=20')
  .then((response) => {
    this.setState({video: response.results})
  })
  .catch(function (error) {
    console.log(error);
  });

此外,您在不需要的渲染函数中直接改变状态,因为无论如何您都在设置状态。还有一件事,我认为您不需要在每次渲染时都获得 api 响应,您可以在 componentWillMount 中执行此操作,如果您希望它是周期性的,请使用 setInterval

import React, { Component } from 'react';;
import axios from 'axios';
class ILotIndex extends Component {

constructor(props) {
    super(props);

    this.state = {video: []};
  }
componentWillMount() {
    setInterval(() => {
         this.VideoList();
    }, 2000)
}
VideoList() {
    axios.get('https://www.googleapis.com/youtube/v3/search?key=AIzaxxxxxxxxxxxxxxxxxxxxx&channelId=UCexxxxxxxxxxxxxxxx&part=snippet,id&order=date&maxResults=20')
  .then((response) => {
    this.setState({video: response.results});
  })
  .catch(function (error) {
    console.log(error);
  });
  }


  render() {
    return (
      <div id="layout-content" className="layout-content-wrapper">
        <div className="panel-list">
          {this.state.video.map((item, i) =>{
            return(
              <h1>{item.items}</h1>

            )
          })}
        </div>
      </div>
    )
  }
}

你犯的一个主要错误是定义你的 React 组件,从小写字符开始,它们应该总是以大写字母开头,否则编译器会尝试在已经定义的标签中搜索它们,比如 div, p, span etc

【讨论】:

  • 我没有安装 axios。请告诉我如何安装?
  • 在我看来this.VideoList()调用应该在componentDidMount()中,这样我们就不需要setTimeout了,等待2-3秒(这里的setTimeout可能很危险,因为我们没有'不知道页面何时完成渲染)。然而,componentDidMount() 确保页面已经被渲染
  • npm install -S axios
  • 我会尝试安装。但我得到了 api 的响应,为什么它没有在页面上呈现?你能帮我理解 {this.state.video.map((item, i) =>{ return(

    {item.items}

    ) })}
  • 能否请您在设置该状态后使用 console.log(this.state.video),正如我在上面评论的那样,componentWillMount() 中的 setTimeout 可能很危险。
猜你喜欢
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 2011-05-21
相关资源
最近更新 更多