Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-google-hooks

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-google-hooks

A lightweight wrapper around Google Places Autocomplete and Directions APIs.

  • 0.2.8
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-87.88%
Maintainers
0
Weekly downloads
 
Created
Source

react-google-hooks

react-google-hooks is a React library that makes it easy to work with Google Places and Geocoding APIs. It provides simple, ready-to-use hooks for fetching place predictions and geocoding information, helping developers quickly integrate these features into their applications. The library is also designed with future expansion in mind, with plans to add support for more Google services through additional hooks.

Installation

Install the library using npm:

npm install react-google-hooks

Prerequisites

Make sure you have an API key from Google Cloud Console with access to the Places API and Geocoding API. Include the Google Maps API script in your index.html file:

<script
  src="https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>&libraries=places"
  async
  defer
></script>

Replace YOUR_API_KEY with your actual API key.

Hooks Overview

The library currently supports the following hooks:

  1. usePlacePredictions - Fetches place predictions based on a query.
  2. useDirections - A service for computing directions between two or more places.

usePlacePredictions Hook

The usePlacePredictions hook allows you to get autocomplete predictions based on user input. It's ideal for enhancing forms or search inputs with location suggestions.

Parameters

The hook accepts an object with the following properties:

ParameterTypeRequiredDefaultDescription
inputstringYesN/AThe text string on which to search for predictions.
inputOffsetnumberNoN/AA zero-based Unicode character offset of input indicating the cursor position. Defaults to the length of the input if not specified.
languagestringNoBrowser's language preferenceThe language in which to return results. Results may be mixed if input language differs from specified language or translation isn't available.
includedPrimaryTypesPlaceType[]NoAll Place typesAn array of up to 5 primary place types (e.g., ['restaurant']). If no types are specified, all Place types are returned.
includedRegionCodesRegionCode[]NoN/AAn array of up to 15 CLDR two-character region codes to filter results. If combined with locationRestriction, results are within the intersection.
locationBiasgoogle.maps.LatLng, google.maps.LatLngLiteral, google.maps.LatLngBounds, google.maps.Circle, stringNoIP-basedBias results to a specific location. At most one of locationBias or locationRestriction should be set.
locationRestrictiongoogle.maps.LatLngBounds, google.maps.LatLngBoundsLiteralNoIP-basedRestrict results to a specific location. At most one of locationBias or locationRestriction should be set.
origingoogle.maps.LatLng, google.maps.LatLngLiteralNoN/AThe origin point for calculating geodesic distance to the destination. If omitted, geodesic distance will not be returned.
regionRegionCodeNoN/AA CLDR two-character region code affecting address formatting, result ranking, and returned results. Does not restrict to the specified region.
sessionTokengoogle.maps.places.AutocompleteSessionTokenNoN/AA token identifying an Autocomplete session for billing purposes. Must generate a new token for each session to avoid incorrect billing.
locationgoogle.maps.LatLngNoN/ALocation for prediction biasing. Predictions will be biased towards the given location and radius. Alternatively, bounds can be used. Deprecated as of May 2023. Use locationBias and locationRestriction instead.
radiusnumberNoN/AThe radius of the area used for prediction biasing in meters. Must always be accompanied by a location property. Alternatively, bounds can be used. Deprecated as of May 2023. Use locationBias and locationRestriction instead.
boundsgoogle.maps.LatLngBounds, google.maps.LatLngBoundsLiteralNoN/ABounds for prediction biasing. Predictions will be biased towards, but not restricted to, the given bounds. Both location and radius will be ignored if bounds is set. Deprecated as of May 2023. Use locationBias and locationRestriction instead.

Returns

An object containing the following properties:

PropertyTypeDescription
predictionsArrayAn array of place prediction objects from the Google Places API.
isLoadingbooleanIndicates if the API request is in progress.
statusstringThe status of the request, which is a key of google.maps.places.PlacesServiceStatus or null if unavailable.

Example Usage

Below is a basic implementation example using the usePlacesPredictions hook:

import React, { useState } from "react";
import { usePlacePredictions } from "react-google-hooks";

const PlacesAutocompleteExample = () => {
  const [query, setQuery] = useState("");
  const { predictions, isLoading, status } = usePlacePredictions({
    input: "Shimla",
    language: "en",
  });

  return (
    <div>
      <input
        type="text"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Search for places..."
      />
      {loading && <p>Loading...</p>}
      <ul>
        {predictions.map((prediction) => (
          <li key={prediction.place_id}>{prediction.description}</li>
        ))}
      </ul>
    </div>
  );
};

export default PlacesAutocompleteExample;

Notes

  • Ensure that the Google Maps JavaScript API is correctly loaded before using this hook.
  • Make sure your API key has the necessary permissions for the Places and Geocoding APIs.

Additional Features

More hooks and features may be added in future versions. If you have any suggestions or encounter any issues, feel free to open a GitHub issue.

License

MIT License

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch.
  3. Commit your changes.
  4. Push the branch and create a pull request.

We appreciate your contributions and feedback to make this library better.

Authors

  • Raghunath Prabhakar (@raghuboi)
  • GitHub Repository
  • Google Cloud Console

Keywords

FAQs

Package last updated on 26 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc