Last active
March 31, 2023 21:25
-
-
Save Manntrix/5f3f4bad8b91d2ec3055aeae1278fc11 to your computer and use it in GitHub Desktop.
This file contains 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
import "./App.css"; | |
import { useState, useEffect } from "react"; | |
import axios from "axios"; | |
function App() { | |
//creating IP state | |
const [ip, setIP] = useState(""); | |
//creating function to load ip address from the API | |
// const getData = async () => { | |
// const res = await axios.get("https://geolocation-db.com/json/"); | |
// console.log(res.data); | |
// setIP(res.data.IPv4); | |
// }; | |
// Updated Code | |
const getData = async () => { | |
const res = await axios.get("https://api.ipify.org/?format=json"); | |
console.log(res.data); | |
setIP(res.data.ip); | |
}; | |
useEffect(() => { | |
//passing getData method to the lifecycle method | |
getData(); | |
}, []); | |
return ( | |
<div className="App"> | |
<h2>Your IP Address is</h2> | |
<h4>{ip}</h4> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment