简体   繁体   English

如何使用 NextJS 的动态导入将值传递给组件

[英]How to pass values to components using dynamic import of NextJS

I have a problem with dynamic import in Next.js.我在 Next.js 中的动态导入有问题。 It would be great if someone could give me an answer or some advice to do this in a different way.如果有人能给我一个答案或一些建议以不同的方式做到这一点,那就太好了。

The thing is that I have a component that renders a leaflet-map, this map have a pointer so I could click the map and have longitude and latitude, this is the code:问题是我有一个渲染传单地图的组件,这个 map 有一个指针,所以我可以点击 map 并有经度和纬度,这是代码:

import React from 'react'
import {MapContainer, Marker,TileLayer, useMapEvents } from 'react-leaflet'
import {  iconMap  } from '../../assets/customeIcon/iconMap';
import 'leaflet/dist/leaflet.css'



const MapView =({selectedPosition,setSelectedPosition}) =>{

    const [initialPosition, setInitialPosition] = React.useState([38,-101]);
    
   
    const Markers = () => {
    
        const map = useMapEvents({
            click(e) {                                
                setSelectedPosition([
                    e.latlng.lat,
                    e.latlng.lng
                ]);              
            },            
        })
    
        return (
            selectedPosition ? 
                <Marker           
                key={selectedPosition[0]}
                position={selectedPosition}
                interactive={false} 
                icon={iconMap}
                />
            : null
        )   
        
    }

    

    return  <MapContainer center={selectedPosition || initialPosition} zoom={5}  style={{height:"300px",width:"540px"}}>
        <TileLayer url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
         ></TileLayer>
         <Markers />
    </MapContainer>
}

export default MapView

As you can see this component has the parameters selectedPosition and setSelectedPosition .如您所见,该组件具有参数selectedPositionsetSelectedPosition This is where I save the clicked position and return it to the parent component.这是我保存单击的 position 并将其返回给父组件的地方。

For example, the parent component used to call the map component this way:例如,父组件曾经这样调用 map 组件:

const Parent = () => {
  const [selectedPosition, setSelectedPosition] = React.useState(null);

  ...

  <MapView selectedPosition={selectedPosition} setSelectedPosition={setSelectedPosition} > </MapView> 
}

This used to work great, but now because of a problem with react-leaflet I have to call the map in a different way, using Next.js dynamic import, I had to create a new component that is like this:这曾经很好用,但现在由于 react-leaflet 的问题,我必须以不同的方式调用 map,使用 Next.js 动态导入,我必须创建一个像这样的新组件:

import dynamic from 'next/dynamic';

function MapCaller() {
    const Map = React.useMemo(() =>  dynamic(
      () => import('./MapView'), 
      { ssr: false, } 
    ), []) 
    return <Map />
  }

export default MapCaller

So now the parent component has to call the MapCaller instead of directly calling the MapView :所以现在父组件必须调用MapCaller而不是直接调用MapView

const Parent = () => {
  const [selectedPosition, setSelectedPosition] = React.useState(null);

  ...

  <MapCaller > </MapCaller> 
}

With this I resolved the problem of react-leaflet, but I have other problem now, remember that I used to pass the position values to the map component, how could I do to pass that values with this new approach?有了这个我解决了 react-leaflet 的问题,但我现在有其他问题,请记住我曾经将 position 值传递给 map 组件,我该如何使用这种新方法传递这些值? How the parent component could communicate with the map to get the selected position?父组件如何与 map 通信以获取选定的 position? Is there another approach to do this?还有另一种方法可以做到这一点吗?

Thanks!谢谢!

Your <MapCaller> component is simply wrapping the existing <MapView> , so you could simply pass the props down to it.您的<MapCaller>组件只是包装现有的<MapView> ,因此您可以简单地将道具传递给它。

const Map = dynamic(() => import('./MapView'), { ssr: false })
 
function MapCaller({ selectedPosition, setSelectedPosition }) {
    return <Map selectedPosition={selectedPosition} setSelectedPosition={setSelectedPosition} />
}

Then use it in the parent component:然后在父组件中使用:

const Parent = () => {
    const [selectedPosition, setSelectedPosition] = React.useState(null);
    //...
    <MapCaller selectedPosition={selectedPosition} setSelectedPosition={setSelectedPosition} /> 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM