码迷,mamicode.com
首页 > Windows程序 > 详细

react jquery方式获取api数据接口

时间:2018-03-14 16:58:43      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:jquer   amount   err   com   mock   turn   body   ror   his   

import React, { Component } from ‘react‘;
import Record from ‘./Record‘;
import { getJSON } from ‘jquery‘;

class Records extends Component {
  constructor() {
    super();
  
    this.state = {
      error: null,
      isLoaded: false,
      records: []
    };
  }

  componentDidMount() {
    getJSON(‘https://5aa780cb7f6fcb0014ee24aa.mockapi.io/api/v1/records‘).then(
      res => {
        this.setState({
          isLoaded: true,
          records: res
        });
      },
      error => {
        this.setState({
          isLoaded: true,
          error
        })
      }
    )
  }

  render() {
    const { error, isLoaded, records } = this.state;
    if (error) {
      return <div>{ error.responseText }</div>;
    } else if (!isLoaded) {
      return <div>loading...</div>
    } else {
      return (
        <div>
          <h2>Records</h2>
          <table className=‘table table-bordered‘>
            <thead>
              <tr>
                <th>Date</th>
                <th>Title</th>
                <th>Amount</th>
              </tr>
            </thead>
            <tbody>
              {records.map((record) => <Record {...record} key={record.id}/>)}
            </tbody>
          </table>
        </div>
      );
    }

    
  }
}

export default Records;

 

react jquery方式获取api数据接口

标签:jquer   amount   err   com   mock   turn   body   ror   his   

原文地址:https://www.cnblogs.com/sunbey/p/8568204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!