I'm trying to build this little project with React Leaflet in which by searching through the leaflet-geosearch search bar a certain city, after clicking on it, a form should pop up asking for some reviews about the place. I want to add a prevent duplicate clause in the handler of the geosearch bar using the array of already rated places that I'm passing to the SearchBar component as a prop. When i try to verify if the cities arr already has an object with the same id, it just doesn't early return, it ignores the early return completely.
CODE///
export default function SearchBar({
onSubmitCityMapSearch,
setCurrLocation,
onSetFocus,
cities,
}) {
function handler(event) {
const {
location: {
label,
y,
x,
raw: { place_id },
},
} = event;
const exists = cities.every((city) => city.location.id !== place_id);
if (!exists) {
console.log("It exists"); /*used to be an early return which wouldn't get executed. In this case the cl does get executed, same as the else block*/
} else {
onSubmitCityMapSearch();
setCurrLocation({ label, location: [y, x], id: place_id });
onSetFocus(place_id);
}
}
const provider = new OpenStreetMapProvider();
const searchControl = new GeoSearchControl({
provider: provider,
style: "bar",
showMarker: false,
});
const map = useMap();
useEffect(() => {
map.addControl(searchControl);
return () => map.removeControl(searchControl);
}, []);
map.on("geosearch/showlocation", handler);
return null;
}
I tried with an if/else statement and in that case it executes both if and else statements. I tried doing some research on it, but to no avail. Anyone has any deeper understanding why this could be happening? The code is a little bit messy, I'm really new to React.