我正在寻找一种在我的应用程序中的任何地方使用相同谷歌地图实例的方法。毕竟每次地图加载都是收费的......
我正在使用google-map-react。在 ComponentDidMount 上创建了一个新的Map实例,所以对我来说,第一个要求是保持该组件安装是有道理的,最有可能靠近组件树的顶部。
最终结果应该是任何想要渲染地图的组件都应该:
- 如果尚未安装地图组件,请安装它。
- 以某种方式回收安装地图的 DOM 节点。
对此的任何帮助将不胜感激。
我正在寻找一种在我的应用程序中的任何地方使用相同谷歌地图实例的方法。毕竟每次地图加载都是收费的......
我正在使用google-map-react。在 ComponentDidMount 上创建了一个新的Map实例,所以对我来说,第一个要求是保持该组件安装是有道理的,最有可能靠近组件树的顶部。
最终结果应该是任何想要渲染地图的组件都应该:
对此的任何帮助将不胜感激。
您可以尝试使用Portals来实现它
门户提供了一种一流的方式来将子级呈现到存在于父组件的 DOM 层次结构之外的 DOM 节点中。
但是,用户对地图所做的每项更改(缩放、图层等)都将保留,因此每当您在另一部分中渲染地图时,您也可以将地图重置为初始状态。
门户确实是正确的方向。我想出了以下方法:
<!-- Root node of React app -->
<div id="root"></div>
<!-- Keep the map here while not in use -->
<div id="map-bench">
<!-- Create the portal using this node as container (2nd argument) -->
<div id="map-container" style="height: 100%; width: 100%"></div>
</div>
return createPortal(renderMap(), document.getElementById("map-container"));
const registerMapOutlet = (rootNode: HTMLDivElement | null) => {
// THE MAP
const mapNode = document.getElementById("map-container");
// Bail out if map node is not found
if (mapNode === null) return;
// on mount: Attach map
if (rootNode) {
rootNode.replaceWith(mapNode);
console.log("REGISTERED MAP OUTLET");
// GoogleMapReact won't load/mount until it is first used
dispatch(mountMapIfNeeded());
}
// on unmount: Return map to bench node
else {
const mapBenchNode = document.getElementById("map-bench");
if (mapBenchNode === null) {
console.warn("Map Bench node was not found");
return;
}
mapBenchNode.appendChild(mapNode);
}
}
// You can pass GoogleMapReact props to this hook
const { registerMapOutlet } = useMapOutlet({});
return <div ref={registerMapOutlet}></div>
google-map-react 1.1.5 版引入了一个shouldUnregisterMapOnUnmount 属性。我会尽快尝试并更新我的答案。