Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
how to set country code automatically by region wise in react?

in this phone number country code how is it possible to set automatically country code by region wise. like if I open the website in USA its shows country code +1 automatically, if I open the website in INDIA its shows country code +91 automatically.Like weather API is there any API that should check automatically country code

import React from "react";
import PhoneInput, {isValidPhoneNumber
} from "react-phone-number-input";
export default function App() {
  const [fields, setFields] = React.useState([{ value: null }]);

  function handleChange(i, event) {
    if (event) {
      const values = [...fields];
      values[i].value = event;
      setFields((v) => values); 
    }
  }

  return (
    <div className="App">
     
      {fields.map((field, idx) => (
        <PhoneInput
          key={idx}
          id="phoneNumbers1"
          type="text"
          placeholder="Enter phone number "
          value={field.value || ""}
          defaultCountry="IN"
          international
          name="phoneNumbers"
          autoComplete="off"
          onChange={(e) => handleChange(idx, e)}
          
        />
      ))}
      <button
        type="button" >
        Phone Number
      </button>
    </div>
  );
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To detect the country, the React app needs to look up the IP address using IP-to-Location services like IPRegistry

Here is a client-side example with IPRegistry :

const getCountryBtn = document.getElementById("getCountry");
const countryNameElement = document.getElementById("country");
getCountryBtn.onclick = function() {
  fetch('https://api.ipregistry.co/?key=tryout')
    .then(function (response) {
        return response.json();
    })
    .then(function (payload) {
        countryNameElement.innerHTML = payload.location.country.name;
    });
}
<html>
<body>
  <button id="getCountry">Get Country</button>
  <div id="country"></div>
</body>
</html>

Once the country name is available, its corresponding country code can be set as the default value in the React component.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!