1.2.6 • Published 2 years ago

react-ip-details v1.2.6

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

react-ip-details

Get user location/ip-details/geo position/currency convert based on api or navigation using simple hook on react

Installation

npm install react-ip-details yarn add react-ip-details

Usage

Here's an example of basic usage:

import React from "react";
import useReactIpLocation from "react-ip-details";

function TimePickerTest() {
  const {
    currency,
    exchangeRate,
    ipResponse,
    exchangeRateResponse,
    errorMessage,
    geoLocationPosition,
    geoLocationErrorMessage,
    currencyString,
  } = useReactIpLocation({ numberToConvert: 100 });
  console.log(`Full ip response ${ipResponse}`);
  console.log(`Full ip exchange-rate response ${exchangeRateResponse}`);
  return (
    <div>
      {`Local currency string format is ${currencyString}`}
      {`Local currency is ${currency}`}
      {`Geo location details: ${geoLocationPosition}`}
      {`Exchange rate is ${exchangeRate}`}
      {`Error Message: ${errorMessage}`}
      {`Geo Error Message: ${geoLocationErrorMessage}`}
    </div>
  );
}

Import Component

import TimePicker from 'react-ip-details'

Props

PropNameTypedefaultdescription
defaultCurrencystringUSDDefault currency (your webpage currency) if u want to make an exchange to local currency
Check currency supported on api used or change url to support it=> https://www.exchangerate-api.com/docs/supported-currencies
detailsByIpUrlstringhttps://geolocation-db.com/json/You can change url to , make sure endpoint return country_code inside response data
exchangeRateUrlstringhttps://api.exchangerate-api.com/v4/latest/You can change url to , make sure ndpoint accept /${currency} addition and return rates in response data
shouldGetIpDetailsbooleantrueWill not use api details request if false.
shouldGetExchangeRatebooleantrueWill not use exchange rate request if false,will keep USD currency only
shouldGetPositionbooleanfalseTurn it to true if u need geolocation info
numberToConvertnumber0If you want to convert number to current currency and format (ex:200 => $100.00(in US) / ALL 10,742(in Albania))
codeCountryToCurrencyobjectjson object (see countryCodeToCurrency.js)You can pass as prop if you want to edit specific country currency for ex : you want to use Eur in Al(Albania)
codeCountryToLocalobjectjson object (see countryCodesToLocal.js)You can pass as prop if you want to edit based on your desire (similar to countryToCurrency)

Return Props

PropNameTypeexampledescription
currencyStringstring$100.00It will return local currency string formated based on currency and exchange rate and numberToConvert
currencystringUSDIt will return local currency based on api at (detailsByIpUrl prop)
exchangeRatestring1.23Exchange rate of default currency to local currency based on ip api and currency exchange api
geoLocationPositionobjectcords: {latitude: 41.342,longitude: 19.811}Get location position based on navigation built in without using api (if you need only this use onlyPosition:true)
ipResponseobjectresponse of api at (endpoint:detailsByIpUrl)
exchangeRateResponseobjectresponse of api at (endpoint:exchangeRateUrl
errorMessagestringMake sure location is allowed by browserIf api fails or anything get wrong it will display messages (messages are static not based on api) replace based on your need
geoLocationErrorMessagestringNo location foundIf navigation location fails to find coords it will display error message