1. 程式人生 > >react中fetch請求

react中fetch請求

1.首先在cmd中下載:

npm install whatwg-fetch

2.在react中引入

import React, { Component }  from 'react'  
import 'whatwg-fetch'

3.請求資料

fetch('https://api.github.com/users/chrissycoyier/repos')
  .then(response => response.json())
  .then(data => console.log('data is', data))
  .catch(error => console.log('error is', error));