简体   繁体   English

来自 React 中其他组件的 Bootstrap Modal 使用

[英]Bootstrap Modal use from other Component in React

This is MapContainer.js这是MapContainer.js

import React, { useEffect, useState } from 'react';
import { GoogleMap, LoadScript, Marker} from '@react-google-maps/api';


const MapContainer = () => {

    const [ currentPosition, setCurrentPosition ] = useState({});
  
    const success = position => {
      const currentPosition = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      }
      setCurrentPosition(currentPosition);
    };
    
    useEffect (() => {
      navigator.geolocation.getCurrentPosition(success);
    })

    const mapStyles = {        
        height: "100vh",
        width: "100%"};
 
  
  return (
     <LoadScript googleMapsApiKey='AIzaSyA40-c3DnhRdFQ5In8xPdTgQSUne1UFhZI'>
       <GoogleMap mapContainerStyle={mapStyles} zoom={13} center={currentPosition}>
          {    
          currentPosition.lat &&
            ( 
              <Marker onClick={""} position={currentPosition}/> 
            )          
          } 
        </GoogleMap>
        
     </LoadScript>
  )
}

export default MapContainer;

And This is ModalBootstrap.js这是ModalBootstrap.js

import { render } from '@testing-library/react';
import React, { useState } from 'react';
import { Modal, Button } from 'react-bootstrap';
import MapContainer from '../MapContainer/MapContainer';


function Example() {
    const [show, setShow] = useState (false);
  
    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);
  
    return (
      <>
        <Button variant="primary" onClick={handleShow}>
          Launch demo modal
        </Button>
  
        <Modal show={show} onHide={handleClose}>
          <Modal.Header closeButton>
            <Modal.Title>Modal heading</Modal.Title>
          </Modal.Header>
          <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
          <Modal.Footer>
            <Button variant="secondary" onClick={handleClose}>
              Close
            </Button>
            <Button variant="primary" onClick={handleClose}>
              Save Changes
            </Button>
          </Modal.Footer>
        </Modal>
      </>
    );
  }
  
  render (<Example />);

  export default Example;

I want to show the 'Modal' when I click on the Marker [in MapContainer.js].I also want to pass the value of latitude and longitude from MapContainer.js to ModalBootstrap.js .当我点击标记 [在 MapContainer.js] 时,我想显示“模态”。我还想将纬度和经度的值从MapContainer.js 传递到 ModalBootstrap.js I want to pass those datas when I click on the Marker.当我点击标记时,我想传递这些数据。 When I click on the Marker a modal will be pop up and it will show the current latitude and longitude of the location.当我点击标记时,会弹出一个模式,它会显示该位置的当前纬度和经度。 I've implemented the google map successfully.我已经成功实施了谷歌地图。 But couldn't pass the value to the ModalBootStrap.js and show the Modal when I click the Marker.但是无法将值传递给 ModalBootStrap.js 并在我单击标记时显示模态。 How can I do this?我怎样才能做到这一点?

Switch your modal to be controlled from a parent state instead of having an internal state, such that the state & state setter for the modal show prop can be controlled by passing these state as props down to the Map Component.将您的模态切换为从父状态控制,而不是具有内部状态,这样就可以通过将这些状态作为道具传递给 Map 组件来控制模态show道具的状态和状态设置器。 An example of these is you can have your Example component & MapContainer component have the same parent component其中一个例子是您可以让您的Example组件和MapContainer组件具有相同的父组件

export default function App() {
  const [show, setShow] = useState(false);

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  const [currentPosition, setCurrentPosition] = useState({});

  return (
    <>
      <MapContainer
        currentPosition={currentPosition}
        setCurrentPosition={setCurrentPosition}
        handleShow={handleShow}
      />
      <Example
        currentPosition={currentPosition}
        show={show}
        handleClose={handleClose}
        handleShow={handleShow}
      />
    </>
  );
}

Back on your MapContainer component just update it to use the handleShow prop for the onClick prop of Marker so it will control the opening of the modal.回到您的MapContainer组件,只需将其更新为对MarkeronClick道具使用handleShow道具,这样它将控制模态的打开。 As for the currentPosition state, we can also moved this state up since this will also be used by the modal as to display the Latitude & Longitude.至于currentPosition状态,我们也可以将这个状态向上移动,因为它也将被模态用于显示纬度和经度。

const MapContainer = ({ currentPosition, setCurrentPosition, handleShow }) => {
  ...
    return (
      <Marker onClick={handleShow} position={currentPosition} />
    )
      ...

Finally, on the modal, just use the passed currentPosition props to display the data as necessary最后,在模态上,只需根据需要使用传递的currentPosition道具来显示数据

function Example({ show, handleShow, handleClose, currentPosition }) {
  return (
    ...
    <Modal.Body>
      Latitude: {currentPosition.lat} Longitude: {currentPosition.lng}
    </Modal.Body>
    ...
  )
}

编辑 React Google Maps getCurrentPosition

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

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