Skip to content

Commit

Permalink
refactor: replace direct MapContext usage with useMap (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-ka authored Apr 11, 2024
1 parent 8a38acf commit 9618024
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/components/advanced-marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ import React, {
Children,
forwardRef,
useCallback,
useContext,
useEffect,
useImperativeHandle,
useMemo,
useState
} from 'react';

import {createPortal} from 'react-dom';
import {GoogleMapsContext} from './map';
import {useMap} from '../hooks/use-map';
import {useMapsLibrary} from '../hooks/use-maps-library';

import type {Ref, PropsWithChildren} from 'react';
import {useMapsLibrary} from '../hooks/use-maps-library';

export interface AdvancedMarkerContextValue {
marker: google.maps.marker.AdvancedMarkerElement;
Expand Down Expand Up @@ -52,7 +51,7 @@ function useAdvancedMarker(props: AdvancedMarkerProps) {
const [contentContainer, setContentContainer] =
useState<HTMLDivElement | null>(null);

const map = useContext(GoogleMapsContext)?.map;
const map = useMap();
const markerLibrary = useMapsLibrary('marker');

const {
Expand Down
12 changes: 3 additions & 9 deletions src/components/info-window.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/* eslint-disable complexity */
import React, {
PropsWithChildren,
useContext,
useEffect,
useRef,
useState
} from 'react';
import React, {PropsWithChildren, useEffect, useRef, useState} from 'react';
import {createPortal} from 'react-dom';

import {GoogleMapsContext} from './map';
import {useMap} from '../hooks/use-map';

/**
* Props for the Info Window Component
Expand All @@ -25,7 +19,7 @@ export type InfoWindowProps = google.maps.InfoWindowOptions & {
export const InfoWindow = (props: PropsWithChildren<InfoWindowProps>) => {
const {children, anchor, shouldFocus, onCloseClick, ...infoWindowOptions} =
props;
const map = useContext(GoogleMapsContext)?.map;
const map = useMap();

const infoWindowRef = useRef<google.maps.InfoWindow | null>(null);
const [contentContainer, setContentContainer] =
Expand Down
5 changes: 2 additions & 3 deletions src/components/marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import React, {
forwardRef,
useCallback,
useContext,
useEffect,
useImperativeHandle,
useState
} from 'react';

import {GoogleMapsContext} from './map';
import {useMap} from '../hooks/use-map';

import type {Ref} from 'react';

Expand All @@ -28,7 +27,7 @@ export type MarkerRef = Ref<google.maps.Marker | null>;

function useMarker(props: MarkerProps) {
const [marker, setMarker] = useState<google.maps.Marker | null>(null);
const map = useContext(GoogleMapsContext)?.map;
const map = useMap();

const {
onClick,
Expand Down

0 comments on commit 9618024

Please sign in to comment.