Skip to content

Instantly share code, notes, and snippets.

@zpuckeridge
Created December 15, 2023 05:30
Show Gist options
  • Select an option

  • Save zpuckeridge/dc6b85bf2e9e8a5f196b001578d6f0c6 to your computer and use it in GitHub Desktop.

Select an option

Save zpuckeridge/dc6b85bf2e9e8a5f196b001578d6f0c6 to your computer and use it in GitHub Desktop.
Retrieve an IP using ipapi.co
"use client";
import { useEffect, useState } from "react";
interface IPData {
ip: string;
city: string;
country_name: string;
}
export default function RetrieveIP() {
const [data, setData] = useState<IPData | null>(null);
useEffect(() => {
const fetchData = async () => {
try {
const res = await fetch("https://ipapi.co/json");
const json = await res.json();
setData(json as IPData);
} catch (error) {
console.error("Error fetching IP data:", error);
setData(null);
}
};
fetchData();
}, []);
return data ? (
<div className="max-w-sm">
<p>
Welcome back ${data.ip}, it's good to see someone from ${data.city}, ${data.country_name}!
</p>
</div>
) : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment