UNPKG

848 BJavaScriptView Raw
1import { useLeafletContext } from '@react-leaflet/core';
2import { useEffect } from 'react';
3export function useMap() {
4 return useLeafletContext().map;
5}
6export function useMapEvent(type, handler) {
7 const map = useMap();
8 useEffect(function addMapEventHandler() {
9 // @ts-ignore event type
10 map.on(type, handler);
11 return function removeMapEventHandler() {
12 // @ts-ignore event type
13 map.off(type, handler);
14 };
15 }, [
16 map,
17 type,
18 handler
19 ]);
20 return map;
21}
22export function useMapEvents(handlers) {
23 const map = useMap();
24 useEffect(function addMapEventHandlers() {
25 map.on(handlers);
26 return function removeMapEventHandlers() {
27 map.off(handlers);
28 };
29 }, [
30 map,
31 handlers
32 ]);
33 return map;
34}