Skip to content

Commit

Permalink
Map refs
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Oct 7, 2020
1 parent 5ae6ac6 commit 3af2473
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions components/map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useRef } from 'react'

import { latlngIsWithinBounds, drawMap, updateStartMarker, updateEndMarker, updatePath, updateMapSize, toggleBikeLockerLayer } from '../lib/map'
const config = require('../frontendconfig.json')
Expand All @@ -7,12 +7,15 @@ const Map = ({ isMobile, mobileView, height, startLocation, endLocation, path, a
const [legendVisible, setLegendVisible] = useState(!isMobile)
const [bikeLockersVisible, setBikeLockersVisible] = useState(false)

const startLocationRef = useRef(startLocation)
const endLocationRef = useRef(endLocation)

const handleMapClick = latlng => {
if (!startLocation) {
if (!startLocationRef.current) {
if (latlngIsWithinBounds(latlng)) {
assignStartLocation(latlng)
}
} else if (!endLocation) {
} else if (!endLocationRef.current) {
if (latlngIsWithinBounds(latlng)) {
assignEndLocation(latlng)
}
Expand Down Expand Up @@ -44,10 +47,12 @@ const Map = ({ isMobile, mobileView, height, startLocation, endLocation, path, a

useEffect(() => {
updateStartMarker(startLocation)
startLocationRef.current = startLocation
}, [startLocation])

useEffect(() => {
updateEndMarker(endLocation)
endLocationRef.current = endLocation
}, [endLocation])

useEffect(() => {
Expand All @@ -58,7 +63,6 @@ const Map = ({ isMobile, mobileView, height, startLocation, endLocation, path, a
updateMapSize()
}, [height])


return (
<div
className="map-container"
Expand Down

1 comment on commit 3af2473

@vercel
Copy link

@vercel vercel bot commented on 3af2473 Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.