get lat and long from address in react js code example

Example: react get lat long of address

// install dependency with npm as seen below
// npm install react-geocode

// IMPORTANT - You will need to insert your Google Maps API on line 15 or this
// will not work, click on the link below to get started with Google Maps Platform
// https://developers.google.com/maps/gmp-get-started

// Don't forget to upvote if this helped

import React, { useState, useEffect } from 'react';
import Geocode from 'react-geocode';

export default function FindLatLong () {

  Geocode.setApiKey('<YOUR API HERE>'); //Insert your Google Maps API here
  Geocode.enableDebug();

  var address = 'Area 51, NV, USA';

  const [lat, setLat] = useState(0)
  const [long, setLong] = useState(0)

  useEffect(() => {
    Geocode.fromAddress(address).then(
      response => {
        setLat(response.results[0].geometry.location.lat);
        setLong(response.results[0].geometry.location.lng);
      }
    )}, []
  )

  return(
    <div>
      <p>Address: {address}</p>
      <p>Lat: {lat}</p>
      <p>long: {long}</p>
    </div>
  )
}