【问题标题】:How to use fetch to post some data如何使用 fetch 发布一些数据
【发布时间】:2018-10-23 14:51:31
【问题描述】:

我只是想知道我是否可以使用 MoviesDB 中的 fetch api 发布数据。我能够将信息发布到控制台。但现在我想在我的 React Page 上实际渲染。下面是我的代码。任何帮助表示赞赏。谢谢!

import React, {Component} from 'react';
import './App.css';




class Home extends Component{

  constructor(props) {
    super(props);

    this.state = {
      posts:{}
    }

  }

  getPost = () => {
    return fetch("https://api.themoviedb.org/3/movie/550?api_key=54a83919a7f93d82a8b8bdd417544d6f")
  .then(res=>res.json())
  .then(posts => console.log(posts))
  }






render() {

    return (
      <div className="App">
      <div className="headings">
      <img className="LOGO" src="https://www.moonlight.com.au/wp-content/themes/moonlight-2016/dist/images/moonlight-logo.png" alt="back"/>
      <input className="input"
        placeholder="Enter a movie title" />
        <button onClick={this.getPost}>Get post</button>
        <div>{JSON.stringify(this.state.posts)}</div>
        </div>
      </div>
    );
    }
}


export default Home;

【问题讨论】:

    标签: reactjs api post get fetch


    【解决方案1】:

    在您的渲染方法中,您使用的是this.state.posts,但您没有在任何地方更新它。

    .then(posts =&gt; console.log(posts)) 替换为.then(posts =&gt; this.setState({ posts }))

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 1970-01-01
      • 2016-05-13
      • 2018-11-24
      • 2022-11-16
      • 2019-01-26
      • 2018-06-25
      • 2019-06-15
      • 1970-01-01
      相关资源
      最近更新 更多