All files / src/hooks useMapboxGeocode.tsx

100% Statements 10/10
100% Branches 0/0
100% Functions 4/4
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2817x 6x                       5x 2x     5x 5x 5x 5x         5x    
import { useEffect, useMemo } from 'react';
import { MapboxGeocode, GeocodeOptions } from '@mapbox/search-js-core';
 
/**
 * A React hook that returns a {@link MapboxGeocode} instance.
 *
 * @param {Options} options
 * @param {string} options.accessToken
 * @see {@link MapboxGeocode}
 */
export function useMapboxGeocode(
  options: Partial<{ accessToken: string } & GeocodeOptions>
): MapboxGeocode {
  const geocode = useMemo(() => {
    return new MapboxGeocode();
  }, []);
 
  useEffect(() => {
    const { accessToken, ...restOptions } = options;
    geocode.accessToken = accessToken;
    geocode.defaults = {
      ...restOptions
    };
  }, [options]);
 
  return geocode;
}