Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
No se puede desestructurar la propiedad 'MapContainer' de 'reactLeaflet' porque es nula

Tengo un componente funcional de React que tiene un mapa de folleto de reacción que se representa en el lado del servidor. El mapa se muestra perfectamente, pero cuando agrego una función de controlador de eventos de clic para pasar al componente principal, aparece el error: No se puede desestructurar la propiedad 'MapContainer' de 'reactLeaflet' porque es nulo. Este es el código en el componente secundario:

 const MyMap = (props, ref) => { const [ reactLeaflet, setReactLeaflet ] = useState(null); const [ position, setPosition ] = useState(null); useEffect(() => { const loadData = async () => { // react-leaflet errors if imported server-side (with "ReferenceError: window is not defined") // not sure if this load can happen in parallel with leaflet.js or not import('react-leaflet') .then(leaflet => setReactLeaflet(leaflet)) .catch(error => noticeError(null, levels.error, error, "react-leaflet failed to load")); }; loadData(); }, []); const { MapContainer, Marker, Popup, TileLayer, useMapEvents, } = reactLeaflet; const HandleClickMap = (e) =>{ const currMap = useMapEvents({ click() { currMap.locate(); }, locationfound(e) { setPosition(e.latlng); currMap.flyTo(e.latlng, currMap.getZoom()); }, }); return position === null ? null : ( <Marker position={position}></Marker> ); }; useImperativeHandle(ref, () => ({ HandleClickMap, })); if (!reactLeaflet) { return <LeafletCss />; } return <> <LeafletCss /> <MapContainer center={{ lat: 0, lng: 0 }} zoom={13} ref={ref} scrollWheelZoom={false} className={style.mapContainer} > <TileLayer attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' /> <HandleClickMap /> </MapContainer> </>; }; export default forwardRef(MyMap);

Este problema ocurrió cuando agregué el gancho useImperativeHandle . Cualquier idea de por qué es apreciada.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Compruebe si el objeto reactLeaflet se ha importado antes de intentar acceder a sus componentes

 const MyMap = (props) => { const { MapContainer, Marker, Popup, TileLayer, useMapEvents, } = props.reactLeaflet; const [ position, setPosition ] = useState(null); const HandleClickMap = (e) =>{ const currMap = useMapEvents({ click() { currMap.locate(); }, locationfound(e) { setPosition(e.latlng); currMap.flyTo(e.latlng, currMap.getZoom()); }, }); return position === null ? null : ( <Marker position={position}></Marker> ); }; useImperativeHandle(props.ref, () => ({ HandleClickMap, })); return <> <LeafletCss /> <MapContainer center={{ lat: 0, lng: 0 }} zoom={13} ref={ref} scrollWheelZoom={false} className={style.mapContainer} > <TileLayer attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' /> <HandleClickMap /> </MapContainer> </>; } const MapLoader = (props, ref) => { const [ reactLeaflet, setReactLeaflet ] = useState(null); useEffect(() => { const loadData = async () => { import('react-leaflet') .then(leaflet => setReactLeaflet(leaflet)) .catch(error => noticeError(null, levels.error, error, "react-leaflet failed to load")); }; loadData(); }, []); return (reactLeaflet ? <MyMap reactLeaflet={reactLeaflet} ref={ref} /> : <LeafletCss /> ); }; export default forwardRef(MapLoader);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda