Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Get client or user ip address in react using axios

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)
  }
  
  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;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Custom usePagination hook 
Javascript :: Renaming props in react 
Javascript :: Paginate array in JavaScript 
Javascript :: Rest and spread operators in ES6 
Javascript :: editorGutter.modifiedBackground striped color 
Javascript :: golang json time 
Javascript :: architecture express.js 
Javascript :: json_populate_recordset 
Javascript :: javascript responsive carousel 
Javascript :: red foreach loop 
Javascript :: discord.js const 
Javascript :: replace div content javascript 
Javascript :: random number between 0 and 50 then round to a whole number 
Javascript :: how to get the last element in an array 
Javascript :: how to get nth tr in js 
Javascript :: conditionally add property to object 
Javascript :: CalendarApp.newRecurrence 
Javascript :: jquery-3.2.1.min.js file download 
Javascript :: how to read from asset in angular 
Javascript :: javascript replace char if not present another character 
Javascript :: xmlhttprequest set route params 
Javascript :: dev console with colored font 
Javascript :: custom render contenful rich text rendering 
Javascript :: string to number javascript & remove text 
Javascript :: dynamic components 
Javascript :: Caret.editorconfig 
Javascript :: launch chrome in incognito and dev tools 
Javascript :: React Hook "useState" is called in function "cardState" which is neither a React function component or a custom React Hook function 
Javascript :: trim para remover excesso de espaço  
Javascript :: react and express 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =