Created
December 15, 2023 05:30
-
-
Save zpuckeridge/dc6b85bf2e9e8a5f196b001578d6f0c6 to your computer and use it in GitHub Desktop.
Retrieve an IP using ipapi.co
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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