我有一个react 应用程序,我有一个谷歌 map 组件.

这一条:

    <GoogleMap
      center={center}
      zoom={3}
      onLoad={(map) => setMap(map)}
      mapContainerStyle={{ width: '100%', height: '100%' }}
    >
       
       {data.toDisplayOnMap.length > 0 && (
          <>
           {data.toDisplayOnMap.map((element, i) => {

             if ('events' in element.item) {
                return <Marker key={i} position={element.item.locationCoords} onClick={() => navigate(`/location?id=${element.id}`)} icon={`http://labs.google.com/ridefinder/images/mm_20_red.png`} />
             } else if ('waypoints' in element.item) {

              if (element.item.traveltype === 'location') {

              return <DirectionsRenderer options={{ preserveViewport: true, polylineOptions: { strokeColor: 'blue', strokeWeight: 2 }, markerOptions: { icon: `http://labs.google.com/ridefinder/images/mm_20_yellow.png` } }} directions={element.route} />
               } else {
                    let wps = element.item.waypoints;;
                    let coords = [];
                    wps.forEach((wp) => {
                        wp.coords.forEach((coord) => {
                             coords.push(coord)
                                                        })
                    })

                                                    
                  return (<>
                   <Marker position={coords[0]} icon={`http://labs.google.com/ridefinder/images/mm_20_yellow.png`} />
                   <Marker position={coords[coords.length - 1]} icon={`http://labs.google.com/ridefinder/images/mm_20_yellow.png`} />
               <Polyline path={coords} options={{ geodesic: true, strokeColor: '#000', strokeOpacity: 1, strokeWeight: 2, editable: false, draggable: false, clickable: true }} />
                   </>)
                }
             }
          })}
        </>
   )}


    </GoogleMap>

现在的情况是,我有一个物体列表,如果人们选中一个复选框,它就会显示在 map 上,但我的中心在非洲,放大了3,这样我就可以最大限度地了解 map .如果我放大以查看该标记或该折线,然后取消选中该复选框,它会重新加载 map 并将我移回非洲.我真的不知道如何将它与useMemo或类似的东西一起使用,因为我 map 上显示的内容来自页面的状态.我试过使用Memo,但可能我用错了,因为它什么都不做.在我的控制台里,我也有这个

253 useJsApiLoader.tsx:62 Performance warning! LoadScript has been reloaded unintentionally!

我是这样宣布我的 map 的: const [map, setMap] = useState(/** @type google.maps.Map */(null))

  const { isLoaded } = useJsApiLoader({
        googleMapsApiKey: ct.MAPS_API_KEY,
        libraries: p
    })

请帮我解决这个问题.

推荐答案

您可以使用REACT钩子useMemo来防止重新呈现组件.您可以这样使用useMemo

const renderGoogleMap = useMemo(() => {
  return <GoogleMap {...props} />;
}, []);

并将此Mememize函数调用为

<div>
  {renderGoogleMap}
</div>

您还可以在更改dep时传递dep以使用Memo钩子,您可以允许重新呈现该视图.

Reactjs相关问答推荐

如何使用next.js以DOM为目标在映射中使用Ref

REACT路由DOM根据参数呈现不同的路由

在一个blog函数中调用setState钩子

使用Reaction钩子窗体验证可选字段

GoLang有条件地为.js.gz提供服务(如果它存在),否则为.js

阻止组件更新的多分派Redux Thunk或setState是否有任何问题

更改点几何体的坐标会将其隐藏

ReactJS 共享上下文

FabricJS反序列化问题

Chakra UI:如何在选中状态下设置复选框 colored颜色

在 React 中将 MUI 样式外包到单独的文件中?

获取更多数据后更新 DOM,而不刷新 React.js 中的页面

显示我是否加入聊天应用程序时出现问题

如何在 React 中停止嵌套 Link 和 Button 组件的事件传播?

这个简单的React组件为什么会多次渲染?

VideoSDK api 不使用更新状态

AWS 全栈应用程序部署教程构建失败

如何在 React 中单击鼠标的位置动态添加 div?

如何通过 Material Ui 设计将 Accordion 添加到您的 React 项目中?

在 React 中使用使用状态挂钩合并两个数组