简体   繁体   English

“IntrinsicAttributes & MapContainerProps”类型上不存在属性“onClick”

[英]Property 'onClick' does not exist on type 'IntrinsicAttributes & MapContainerProps'

Onclick doesn't work on the mapcontainer, can anyone help with that? Onclick 在地图容器上不起作用,有人可以帮忙吗?

Function handleMapClick: Function手柄图点击:

function handleMapClick(event: LeafletMouseEvent) {
    const { lat, lng } = event.latlng;

    setPosition({
      latitude: lat,
      longitude: lng,
    });
  }

MapContainer with onClick:带有 onClick 的 MapContainer:

<MapContainer 
              center={[-29.9518154,-51.1689972]}
              style={{ width: '100%', height: 280 }}
              zoom={15}
              onClick={handleMapClick}
            >
import React, { useState } from "react";
import { MapContainer, Marker, TileLayer, useMapEvents } from 'react-leaflet';
import Map from 'react-leaflet';
import { LeafletMouseEvent } from 'leaflet';

import { FiPlus } from "react-icons/fi";

import Sidebar from "../components/Sidebar";
import mapIcon from "../utils/mapIcon";

import '../styles/pages/create-ong.css';

export default function CreateOng() {
  const [position, setPosition] = useState({ latitude: 0, longitude: 0 })

  function handleMapClick(event: LeafletMouseEvent) {
    const { lat, lng } = event.latlng;

    setPosition({
      latitude: lat,
      longitude: lng,
    });
  }

  return ( 
    <div id="page-create-ong">
      <Sidebar />

      <main>
        <form className="create-ong-form">
          <fieldset>
            <legend>Dados</legend>

            <MapContainer 
              center={[-29.9518154,-51.1689972]}
              style={{ width: '100%', height: 280 }}
              zoom={15}
              onClick={handleMapClick}
            >
              <TileLayer 
                url={`https://api.mapbox.com/styles/v1/mapbox/light-v10/tiles/256/{z}/{x}/{y}@2x?access_token=${process.env.REACT_APP_MAPBOX_TOKEN}`}
              />

              { position.latitude !== 0 
                ? <Marker interactive={false} icon={mapIcon} position={[position.latitude, position.longitude]} />
                : null
              }
              
            </MapContainer>

            <div className="input-block">
              <label htmlFor="name">Nome</label>
              <input id="name" />
            </div>

            <div className="input-block">
              <label htmlFor="about">Sobre <span>Máximo de 300 caracteres</span></label>
              <textarea id="name" maxLength={300} />
            </div>

            <div className="input-block">
              <label htmlFor="images">Fotos</label>

              <div className="uploaded-image">

              </div>

              <button className="new-image">
                <FiPlus size={24} color="#15b6d6" />
              </button>
            </div>
          </fieldset>

          <fieldset>
            <legend>Visitação</legend>

            <div className="input-block">
              <label htmlFor="instructions">Instruções</label>
              <textarea id="instructions" />
            </div>

            <div className="input-block">
              <label htmlFor="opening_hours">Nome</label>
              <input id="opening_hours" />
            </div>

            <div className="input-block">
              <label htmlFor="open_on_weekends">Atende fim de semana</label>

              <div className="button-select">
                <button type="button" className="active">Sim</button>
                <button type="button">Não</button>
              </div>
            </div>
          </fieldset>

          <button className="confirm-button" type="submit">
            Confirmar
          </button>
        </form>
      </main>
    </div>
  );
}

You can add OnClick handler to <MapContainer /> using useMapEvent hook:您可以使用useMapEvent挂钩将OnClick处理程序添加到<MapContainer />

function handleMapClick(event: LeafletMouseEvent) {
  const { lat, lng } = event.latlng;

  setPosition({
    latitude: lat,
    longitude: lng,
  });
}

...

useMapEvent('click', handleMapClick)

Full example: https://react-leaflet.js.org/docs/example-react-control/完整示例: https://react-leaflet.js.org/docs/example-react-control/

If you are familiar with components in react then use class components.如果您熟悉 react 中的组件,请使用 class 组件。 This is best for changing states of different elements on a webpage这最适合更改网页上不同元素的状态

暂无
暂无

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

相关问题 类型 IntrinsicAttributes 和字符串 [] 上不存在属性“道具” - Property 'props' does not exist on type IntrinsicAttributes & string[] 类型“IntrinsicAttributes & Props”上不存在属性“...” - Property '...' does not exist on type 'IntrinsicAttributes & Props' React forwardRef - 类型 IntrinsicAttributes 上不存在属性 - React forwardRef - Property does not exist on type IntrinsicAttributes 如何解决“IntrinsicAttributes &amp; Function”类型上不存在的类型属性“”? - How to solve Type Property '' does not exist on type 'IntrinsicAttributes & Function'? 类型/ IntrinsicAttributes和IntrinsicClassAttributes上不存在React Typescript属性 - React Typescript property does not exist on type / IntrinsicAttributes & IntrinsicClassAttributes React 属性在类型“IntrinsicAttributes”(自定义钩子)上不存在 - React Property does not exist on type 'IntrinsicAttributes' (custom hook) React TypeScript 和 ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref” - React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes 在“IntrinsicAttributes”类型错误中不存在获取属性“ref” - Getting Property 'ref' does not exist on type 'IntrinsicAttributes' error React & Slate 出现 TypeScript 错误:属性“renderElement”在类型“IntrinsicAttributes”上不存在 - TypeScript error with React & Slate: Property 'renderElement' does not exist on type 'IntrinsicAttributes' React native ref Property 'ref' 在类型'IntrinsicAttributes &amp; 上不存在 - React native ref Property 'ref' does not exist on type 'IntrinsicAttributes &
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM