Skip to content

Instantly share code, notes, and snippets.

@Manntrix
Last active March 31, 2023 21:25
Show Gist options
  • Save Manntrix/5f3f4bad8b91d2ec3055aeae1278fc11 to your computer and use it in GitHub Desktop.
Save Manntrix/5f3f4bad8b91d2ec3055aeae1278fc11 to your computer and use it in GitHub Desktop.
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