Skip to content

Commit

Permalink
Create custom hooks useMap
Browse files Browse the repository at this point in the history
  • Loading branch information
flexbox committed Apr 24, 2019
1 parent 543a040 commit fc0df85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
21 changes: 6 additions & 15 deletions 2-custom-hooks-and-useDebugValue/app/src/Map.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import React, { useRef, useEffect } from "react";
import Leaflet from "leaflet";
import "leaflet/dist/leaflet.css";
import React from 'react';
import 'leaflet/dist/leaflet.css';
import useMap from './useMap';

function Map() {
const element = useRef(null);

useEffect(() => {
const map = Leaflet.map(element.current).setView([51.505, -0.09], 13);

Leaflet.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
});
const mapElement = useMap();

return (
<div
ref={element}
style={{ width: 600, height: 400, backgroundColor: "#ddd" }}
ref={mapElement}
style={{ width: 600, height: 400, backgroundColor: '#ddd' }}
/>
);
}
Expand Down
19 changes: 19 additions & 0 deletions 2-custom-hooks-and-useDebugValue/app/src/useMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useRef, useEffect } from 'react';
import Leaflet from 'leaflet';

function useMap() {
const mapElement = useRef(null);

useEffect(() => {
const map = Leaflet.map(mapElement.current).setView([51.505, -0.09], 13);

Leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}, []);

return mapElement;
}

export default useMap;

0 comments on commit fc0df85

Please sign in to comment.