变焦未更新为13,我希望在将函数handel缩放传递给子组件时,缩放变量在父组件中更改,缩放仍然等于1,找到了位置,但函数handel缩放不以13作为值。
家长:
import DraggableMarker from './draggablemarker/draggablemarker'
import React, {useState,useRef, useMemo,useCallback, useEffect} from "react";
import { MapContainer, TileLayer, Marker, Popup,useMapEvents } from 'react-leaflet';
import MinimapControl from './minimapcontrol/minimapcontrol'
import LocationMarker from './positionmarker/positionmarker'
import './App.css';
function App() {
const POSITION_CLASSES = {
bottomleft: 'leaflet-bottom leaflet-left',
bottomright: 'leaflet-bottom leaflet-right',
topleft: 'leaflet-top leaflet-left',
topright: 'leaflet-top leaflet-right',
}
const BOUNDS_STYLE = { weight: 1 }
const [map, setMap] = useState(null)
const [zoom,setzoom]=useState(1)
const center = [51.505, -0.09]
function handlezoom(zoomi){
//️ take parameter passed from Child component
setzoom(zoomi)};
return(
<div>
<MapContainer center={center} zoom={zoom} scrollWheelZoom={false}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{/* <DraggableMarker /> */}
<LocationMarker handlezoom={handlezoom} />
{/* <DraggableMarker /> */}
<MinimapControl position="topright" />
</MapContainer>
</div>
)
}
export default App;儿童:
import React,{useState} from 'react'
import { MapContainer, TileLayer, Marker, Popup,useMapEvents } from 'react-leaflet';
export default function LocationMarker({handlezoom}) {
const [position, setPosition] = useState(null)
const [haveuserlocation,sethaveuserlocation]= useState(false)
const map = useMapEvents({
click() {
map.locate()
handlezoom(13)
},
locationfound(e) {
setPosition(e.latlng)
map.flyTo(e.latlng, map.getZoom())
},
})
return (position === null ? null :
<Marker position={position}>
<Popup>You are here</Popup>
</Marker>
)
}首先要变焦值为1,在找到位置后,它的值为13,请帮助我。

发布于 2022-10-05 23:29:13
pass setZoom
...
<LocationMarker setZoom={setZoom} />
...
export default function LocationMarker({setZoom}) {
click() {
map.locate()
setZoom(13)
}https://stackoverflow.com/questions/73966958
复制相似问题