【问题标题】:Fetch API data using React使用 React 获取 API 数据
【发布时间】:2018-05-14 03:43:02
【问题描述】:

我有一个反应,它使用 django rest 框架 API。我要获取 JSON 数据,但似乎我没有正确获取信息或者我没有以正确的方式呈现:

import React, { Component } from 'react' ;
class App extends Component {
  state = {
    todos: []
  };
async componentDidMount() {

fetch('http://127.0.0.1:8000/api/todos/')
.then(results =>{
  console.log(results)
  const get_todos = results.map( c=>{
    return {
      id: c.id,
      title: c.title,
      descripttion: c.title
    };
  });

  const newstate = Object.assign({},this.state,{
      todos: get_todos
    });
    this.setState(newstate);
  }).catch(error=> console.log(error));
}
render(){
  return (
    <div className="App">
       {this.state.todos} 
    </div>
  )
 }
}
export default App;

【问题讨论】:

    标签: reactjs django-rest-framework


    【解决方案1】:

    应该是

    state = { loading : true }
    componentDidMount() {
     fetch('http://127.0.0.1:8000/api/todos/')
      .then(blob => blob.json())
      .then(response => {
        ...
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 2018-12-07
      • 1970-01-01
      • 2019-09-16
      • 2018-01-31
      • 2023-03-20
      • 2021-11-14
      • 2020-04-16
      • 2019-01-05
      相关资源
      最近更新 更多