## Description
When clicking on a marker in the Map Widget, the onMarkerClick event was
being triggered twice for a single click, causing duplicate actions and
potentially unexpected behavior in applications.
```js
// In MapWidget/widget/index.tsx
onMarkerClick = (lat?: number, long?: number, title?: string) => {
this.disableDrag(true);
const selectedMarker = {
lat: lat,
long: long,
title: title,
};
console.log("🚀 ~ MapWidget ~ title:", title); // This was logging twice
this.props.updateWidgetMetaProperty("selectedMarker", selectedMarker, {
triggerPropertyName: "onMarkerClick",
dynamicString: this.props.onMarkerClick,
event: {
type: EventType.ON_MARKER_CLICK,
},
});
};
```
## Root Cause
This happens because:
1. The Marker component was adding duplicate event listeners to the
Google Maps marker.
2. Two separate useEffect hooks were both adding "click" event
listeners:
- One during marker initialisation
- Another in a separate useEffect that tracks the onClick prop
```js
// First listener in initialization useEffect
googleMapMarker.addListener("click", () => {
if (onClick) onClick();
});
// Second listener in a separate useEffect
marker.addListener("click", () => {
if (onClick) onClick();
});
```
3. When a marker was clicked, both event listeners fired, causing the
onClick callback to execute twice.
4. This resulted in onMarkerClick being called twice for a single user
interaction.
## Solution
Modify the `Marker.tsx` component to avoid adding duplicate event
listeners:
1. Remove the click event listener from the initialisation useEffect
2. Add proper cleanup for event listeners using clearListeners and
removeListener
3. Store the listener reference to properly remove it in the cleanup
function
4. Apply the same pattern to the dragend event listener
```js
// track on onclick - with proper cleanup
useEffect(() => {
if (!marker) return;
// Clear existing click listeners before adding a new one
google.maps.event.clearListeners(marker, "click");
// Add the new click listener
const clickListener = marker.addListener("click", () => {
if (onClick) onClick();
});
// Return cleanup function
return () => {
google.maps.event.removeListener(clickListener);
};
}, [marker, onClick]);
```
## Why this works
1. Properly cleans up existing listeners before adding new ones
2. Ensures only one click handler is active at any time
3. Prevents event handler duplication during component re-renders
## Testing
Added a Cypress test that verifies the marker click event triggers
exactly once per click:
1. Creates a map widget with a marker
2. Uses a JS Object to track click count
3. Verifies the click count increments by exactly 1 for each click
4. Ensures no duplicate events are triggered
Fixes #39514
## Automation
/ok-to-test tags="@tag.Widget, @tag.Sanity, @tag.Maps, @tag.Binding"
### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/13673511245>
> Commit: 3c025ebd9dd85a3c409bc1582e13e324d8dbb2cd
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=13673511245&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Widget, @tag.Sanity, @tag.Maps, @tag.Binding`
> Spec:
> <hr>Wed, 05 Mar 2025 11:49:24 UTC
<!-- end of auto-generated comment: Cypress test results -->
## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **Bug Fixes**
- Enhanced the behavior of map markers so they respond more reliably to
user interactions like clicking and dragging, addressing issues that
could affect overall stability.
- **Tests**
- Added comprehensive tests to ensure marker functionality remains
consistent and dependable during use.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
|
||
|---|---|---|
| .github | ||
| app | ||
| contributions | ||
| deploy | ||
| scripts | ||
| static | ||
| .coderabbit.yaml | ||
| .deepsource.toml | ||
| .editorconfig | ||
| .env.example | ||
| .gitignore | ||
| .imgbotconfig | ||
| .nojekyll | ||
| app.json | ||
| appsmith_events.md | ||
| appsmith_templates.md | ||
| ci-debug.sh | ||
| CODE_OF_CONDUCT.md | ||
| CODEOWNERS | ||
| CONTRIBUTING.md | ||
| depot.json | ||
| Dockerfile | ||
| heroku.yml | ||
| IssuesReport.md | ||
| LICENSE | ||
| README.md | ||
| SECURITY.md | ||
Organizations build internal applications such as dashboards, database GUIs, admin panels, approval apps, customer support dashboards, and more to help their teams perform day-to-day operations. Appsmith is an open-source tool that enables the rapid development of these internal apps. Read more on our website.
Installation
There are two ways to start using Appsmith:
- Signup on Appsmith Cloud.
- Install Appsmith on your machine. See the installation guides below.
| Installation Methods | Documentation |
|---|---|
| Docker (Recommended) | |
| Kubernetes | |
| AWS AMI |
For other deployment options, see the Installation Guides documentation.
Development
To build and run Appsmith in your local dev environment, see Setup for local development.
Learning Resources
Need Help?
Contributing
We ❤️ our contributors. We're committed to fostering an open, welcoming, and safe environment in the community.
📕 We expect everyone participating in the community to abide by our Code of Conduct. Please read and follow it.
🤝 If you'd like to contribute, start by reading our Contribution Guide.
👾 Explore some good first issues.
Let's build great software together.
Top Contributors
License
Appsmith is licensed under the terms of Apache License 2.0.
