From 8198f8df087eb24fdc679e8e2992227e9103f5ee Mon Sep 17 00:00:00 2001 From: Xiaoji Chen Date: Mon, 1 Feb 2021 11:27:36 -0800 Subject: [PATCH] Fix bug where getMap() at component mount returns null (#1319) --- src/components/static-map.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/components/static-map.js b/src/components/static-map.js index d8adac5a..1148c1dd 100644 --- a/src/components/static-map.js +++ b/src/components/static-map.js @@ -18,15 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. import * as React from 'react'; -import { - useState, - useRef, - useEffect, - useContext, - useMemo, - useImperativeHandle, - forwardRef -} from 'react'; +import {useState, useRef, useContext, useMemo, useImperativeHandle, forwardRef} from 'react'; import * as PropTypes from 'prop-types'; import WebMercatorViewport from 'viewport-mercator-project'; @@ -108,10 +100,11 @@ function NoTokenWarning() { ); } -function getRefHandles(map) { +function getRefHandles(mapboxRef) { return () => ({ - getMap: () => map, + getMap: () => mapboxRef.current && mapboxRef.current.getMap(), queryRenderedFeatures: (geometry, options = {}) => { + const map = mapboxRef.current && mapboxRef.current.getMap(); return map && map.queryRenderedFeatures(geometry, options); } }); @@ -129,7 +122,7 @@ const StaticMap = forwardRef((props, ref) => { const containerRef = useRef(null); const context = useContext(MapContext); - useEffect(() => { + useIsomorphicLayoutEffect(() => { if (!StaticMap.supported()) { return undefined; } @@ -191,7 +184,7 @@ const StaticMap = forwardRef((props, ref) => { // Note: this is not a recommended pattern in React FC - Keeping for backward compatibility useImperativeHandle( ref, - useMemo(() => getRefHandles(map), [map]) + useMemo(() => getRefHandles(mapboxRef), []) ); const overlays = map && (