你可以这样做:
import React from 'react';
import fetch from 'isomorphic-unfetch';
// Vercel has created a data-fetching library called SWR (client side).
import useSWR from 'swr';
const API_URL = 'https://extreme-ip-lookup.com/json/';
async function fetcher(url) {
const res = await fetch(url);
const json = await res.json();
return json;
}
function Index() {
const { data, error } = useSWR(API_URL, fetcher);
if (error) return <div>failed to load</div>;
if (!data) return <div>loading...</div>;
const { countryCode } = data;
return (
<div>
<p>Country Code: {countryCode}</p>
</div>
);
}
export default Index;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4876 次 |
| 最近记录: |