feat: Get the ip of the user

This commit is contained in:
2023-06-22 16:08:15 +02:00
parent f996401165
commit 64d28d0591
3 changed files with 75 additions and 24 deletions

View File

@ -1,33 +1,26 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import { useState, useEffect } from 'react'
import './App.css'
import axios from 'axios'
function App() {
const [count, setCount] = useState(0)
const [ip, setIP] = useState('')
const getData = async () => {
const res = await axios.get('https://api.ipify.org?format=json');
console.log(res.data.ip)
setIP(res.data.ip);
}
useEffect(() => {
getData();
}, [])
return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
<div className='App'>
<h2>Your IP Address is </h2>
<h4>{ip || "loading"}</h4>
</div>
</>
)
}