简体   繁体   English

React 将 function 作为 prop 传递给子组件,但父组件未访问子组件提供的数据

[英]React Passing a function as prop to child component, but the parent is not accessing the data provided by the child

the zoom is not updated into 13 i want the zoom variable to be changend in the parent component when passing the function handelzoom to the child component the zoom is still equal to 1, the position is found but the function handelzoom doesnt take 13 as a value缩放未更新为 13 我希望在将 function handelzoom 传递给子组件时更改父组件中的缩放变量缩放仍然等于 1,找到 position 但 function handelzoom 不将 13 作为值

the parent:父母:

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='&copy; <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;

the child:孩子:

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>
  )
  
  
    
  
}

want the zoom to have value of 1 at first and after finding the position it will take the value of 13 please help me首先希望缩放的值为 1,在找到 position 后它将取值为 13,请帮助我在此处输入图像描述

pass setZoom通过设置缩放

...
  <LocationMarker setZoom={setZoom} />
...


export default function LocationMarker({setZoom}) {


click() {
  map.locate()
  setZoom(13)
}

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

相关问题 从父组件传递道具在子组件 React 中变为空 - Passing a prop from a Parent component becomes null in the Child component React JavaScript React:将道具从子组件中的 function 传递到父组件中的 function 时出现问题 - JavaScript React: Issues with passing prop from a function in a child component to a function in the parent 将数据从子级传递到父级组件-反应-通过回调函数 - passing data from child to parent component - react - via callback function 将 prop 从父组件传递给子组件时出错 - Error with passing a prop from parent to child component ReactJS,将prop从父组件传递到子组件? - ReactJS, passing prop from parent to child component? 将 function 作为道具传递给 React 中的子组件会引发错误 - passing function as a prop to child component in React throws error 使用React和Gatsby将道具从父母传递给孩子 - Passing a prop to a child from parent with React and Gatsby 通过onClick反应将数据从子组件传递到父组件 - React Passing Data From Child Component to Parent Component via a onClick 在本机反应中将数据从子组件传递到父组件? - Passing data from child component to parent component in react native? 在嵌套组件反应js中在子组件和父组件之间传递数据 - passing data between child and parent component in nested component react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM