Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
IP address finder app using ReactJS
Next article icon

IP address finder app using ReactJS

Last Updated : 19 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will be building an IP address finder app that lets you find your client's approximate location on a map. An IP address is a unique address that identifies a device on the internet or a local network. IP stands for "Internet Protocol," which is the set of rules governing the format of data sent via the internet or local network. Through this article, we will learn how to get the user's IP address as well as display his/her approximate location on a map.

Lets take a look at how the final application will look like:

 

Prerequisites: The pre-requisites for this project are:

  • React
  • Functional Components
  • React Hooks
  • Axios & APIS
  • Javascript ES6

Creating a React application and Module installation:

Step 1: Create a react application by typing the following command in the terminal:

npx create-react-app ip-finder

Step 2: Now, go to the project folder i.e ip-finder by running the following command:

cd ip-finder

Step 3: Install some npm packages required for this project using the following command:

npm install axios   npm install react-map-gl  npm install react-icons

Project Structure: It will look like this:

Example: Let us grab the Mapbox API key required for this project. Follow the simple steps below:

  • Go to the website: https://www.mapbox.com/ and create a free account.
  • Go to your account dashboard and at the bottom of the page you will find a section named "Access tokens".

Copy the default public access token and save it somewhere to use it later.

Example:

  • App.js: In this file, we will fetch the user's IP address using a free open-source API named https://ipapi.co/ (free plan: 1000 requests/day). 
  • Map.js: Now write down the following code in the Map.js component.
    Note: Replace <YOUR_API_KEY> with your own Mapbox public access token.
  • App.css: This file contains the styling of the webpages.
JavaScript
// App.js  import { useEffect, useState } from 'react'; import Axios from 'axios'; import Map from './components/Map'; import './App.css';  function App() {      // Setting up the initial state variables     const [ipDetails, setIpDetails] = useState([]);     const [lat, setLat] = useState(22.5726);     const [lon, setLon] = useState(88.3832);      // Fetching the API once when the     // component is mounted     useEffect(() => {         Axios.get('https://ipapi.co/json/').then((res) => {             setIpDetails(res.data);             setLat(res.data.latitude);             setLon(res.data.longitude);         });     }, [])      return (         <>             <h1 className="heading">IP Address Finder</h1>             <div className="App">                 <div className="left">                     <h4>What is my IPv4 address?</h4>                     <h1 id="ip">{ipDetails.ip}</h1>                     <h4>Approximate location: </h4>                      <p>{ipDetails.city}, {ipDetails.region},                         {ipDetails.country_name}.</p>                       <h4>Internet Service Provider(ISP):</h4>                      <p>{ipDetails.org}</p>                  </div>                 <Map lat={lat} lon={lon} />             </div>         </>     ); }  export default App; 
JavaScript
// Map.js  import React, { useEffect, useState } from 'react'; import ReactMapGL, { Marker } from 'react-map-gl'; import { RiUserLocationFill } from 'react-icons/ri';  const API_KEY = '<YOUR_API_KEY>';  const Map = ({ lat, lon }) => {      // Setting up the initial viewport of the map     const [viewport, setViewport] = useState({         latitude: lat,         longitude: lon,         zoom: 14,         bearing: 0,         pitch: 0,         width: '100%',         height: '100%',     });      // Viewport re-renders whenever latitude     // and longitude changes     useEffect(() => {         const a = { ...viewport };         a.latitude = lat;         a.longitude = lon;         setViewport(a);     }, [lat, lon]);      return (         <div className="map">             <ReactMapGL                 mapboxApiAccessToken={API_KEY}                 {...viewport}                 onViewportChange={(viewport) => setViewport(viewport)}                 mapStyle="mapbox://styles/mapbox/streets-v11">                 <Marker latitude={lat} longitude={lon}>                     <div className="mark">                         <RiUserLocationFill size="25px" color="blue" />                     </div>                 </Marker>             </ReactMapGL>         </div>     ); };  export default Map; 
CSS
/* App.js */ @import url('https://fonts.googleapis.com/css2?family=Pacifico&display=swap');  .App {     height: 70vh;     display: flex;     justify-content: center;     align-items: center; }  .left {     box-sizing: border-box;     padding-left: 80px;     width: 40%; }  .map {     width: 550px;     height: 350px;     border: 4px solid #1453dc; }  .mark {     padding: 40px;     border-radius: 50%;     background-color: #d2d8fabe }  .heading {     font-size: 60px;     text-align: center;     text-decoration: underline;     font-family: 'Pacifico', cursive;     color: #1453dc; }  p {     font-size: 20px;     color: #1453dcaf; }  #ip {     color: #1453dc; } 

Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

App demo

Next Article
IP address finder app using ReactJS

A

anomic30
Improve
Article Tags :
  • Project
  • Web Technologies
  • ReactJS
  • Web Development Projects
  • ReactJS-Projects

Similar Reads

    Domain name finder using ReactJS
    In this article, we are going to make a domain name generator app that generates plenty of domain names related to our input keyword available to buy. In this domain name generator, we will take a text given by the user and will generate plenty of names related to the input text given, and by clicki
    4 min read
    How to create a Location finder app using ReactJS ?
    In this article, we will be building a location finder app that lets you search for different places on a map. Our app contains two sections, one for displaying the marker on the map and the other is for searching different places. For rendering the map and getting the coordinates for the searched l
    4 min read
    Build an Admin Panel using ReactJS
    The Admin Panel project is a web application built using ReactJS, a popular JavaScript library for building user interfaces. The Admin Panel is designed to provide a centralized and user-friendly interface. It provides a user interface for managing various aspects of a platform, such as viewing rece
    9 min read
    Movie Search Engine Using React and API
    In this article, we will create Movie Search Engine using ReactJS. In this movie search engine the user can search for movies which they want to know about and the search engine will fetch a list of movies from that keyword and display the result in the forms of card. A default search has been loade
    6 min read
    Recipe Finder using ReactJS
    In this project article, we will be creating a recipe finder application using the React library. We have given the application the name "GeeksforGeeks Recipe Finder". This application is completely developed in ReactJS and mainly focuses on the functional components and also manages the various sta
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences