Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

162 user not always seeing heatmap legend reset to city after mousing out of heatmap neighborhoods 1 #167

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/Components/IntensityMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function IntensityMap({
.attr("stroke", "#f0f0f0")
.attr("fill", (feature) => colorScale(data[feature.id] ?? 0))
.on("mouseover", (_, feature) => onMouseOver(feature))
.on("mouseout", () => onMouseOver());
.on("mouseout", () => onMouseOver(null));
}
}, [data, map, width, height, minColor, maxColor, onMouseOver]);

Expand Down
16 changes: 14 additions & 2 deletions client/src/Users/UsersList.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function UsersList() {
const [usersByZipActive, setUsersByZipActive] = useState();
const [usersByZipMedianSteps, setUsersByZipMedianSteps] = useState();
const [selectedFeature, setSelectedFeature] = useState();
const [resetTimeoutId, setResetTimeoutId] = useState(null);

useEffect(() => {
let cancelled = false;
Expand Down Expand Up @@ -152,7 +153,18 @@ function UsersList() {
}

function onMouseOverZip(feature) {
setSelectedFeature(feature);
if (resetTimeoutId) {
clearTimeout(resetTimeoutId);
}
setSelectedFeature(feature);


const newTimeoutId = setTimeout(() => {
setSelectedFeature(null);
}, 100);

setResetTimeoutId(newTimeoutId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Introducing this new state variable actually has more significant side effects than first appears. Because this function is dependent upon the variable, every time the variable is updated with the setter, it's going to trigger a redraw of the component and, because the map depends on this function, a redraw of the map.

The result is that, when the mouse is over the map, the browser enters into effectively an infinite loop of redraws until the mouse leaves the map. This is not really an efficient way to have the app run. To confirm this, add a console.log into this function. You'll see it print out repeatedly non-stop when the mouse is over a map.


}

let totalUsers;
Expand Down Expand Up @@ -262,7 +274,7 @@ function UsersList() {
</div>
<div className="col-lg-2">
<h4>
{!selectedFeature && "San Franciso"}
{!selectedFeature && "San Francisco"}
{selectedFeature &&
`${selectedFeature.properties.neighborhood} (${selectedFeature.id})`}
</h4>
Expand Down