I can't set and change the marker on click.
I get the user's location from the browser and center the map with these coordinates. When I run console.log(coords) I can see the coordinates that the browser picked up, but when I go to set the marker using [formValues.coords[0], formValues.coords[1]] as LatLngExpression it doesn't work. When I click on the map it's not changing the coordinates either. I am doing something wrong, but I haven't figured out where yet.
Another thing I saw is that the whenCreated property has been removed from react-leaflet v4:
"Removed whenCreated property from the MapContainer component (a ref callback can be used instead)."
But I don't know how to use this ref callback in this context.
Can anyone help me? Here is the code in the map rendering part:
import {
Button,
ButtonContainer,
CategoryBox,
CategoryContainer,
CategoryImage,
Container,
Form,
FormTitle,
MapContainer,
Section,
} from "./styles";
import Input from "../../components/Input";
import { useState } from "react";
import { LatLngExpression, LeafletMouseEvent } from "leaflet";
import { TileLayer, Marker } from "react-leaflet";
import { categories } from "./categories";
import useGetLocation from "../../hooks/useGetLocation";
import { toast } from "react-toastify";
import { useNavigate } from "react-router-dom";
export default function New() {
const navigate = useNavigate();
const [formValues, setFormValues] = useState({
name: "",
description: "",
contact: "",
category: "",
coords: [0, 0],
});
const { coords } = useGetLocation();
console.log(coords);
async function onSubmit() {
const request = await fetch("http://localhost:5000/store", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...formValues,
latitude: formValues.coords[0],
longitude: formValues.coords[1],
}),
});
if (request.ok) {
toast("Estabelecimento gravado com sucesso!", {
type: "success",
autoClose: 2000,
onClose: () => navigate("/"),
});
}
}
if (!coords) {
return <h1>Obtendo localização...</h1>;
}
return (
<Container>
<Form
onSubmit={(ev) => {
ev.preventDefault();
onSubmit();
}}
>
<FormTitle>Cadastro do comércio local</FormTitle>
<Section>Dados</Section>
<Input
label="Nome do local"
name="name"
value={formValues.name}
onChange={setFormValues}
/>
<Input
label="Descrição"
name="description"
value={formValues.description}
onChange={setFormValues}
/>
<Input
label="Contato"
name="contact"
value={formValues.contact}
onChange={setFormValues}
/>
<Section>Endereço</Section>
<MapContainer
center={
{
lat: coords[0],
lng: coords[1],
} as LatLngExpression
}
zoom={13}
whenCreated={(map) => {
map.addEventListener("click", (event: LeafletMouseEvent) => {
setFormValues((prev) => ({
...prev,
coords: [event.latlng.lat, event.latlng.lng],
}));
});
}}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker
position={
[formValues.coords[0], formValues.coords[1]] as LatLngExpression
}
/>
</MapContainer>
<Section>Categoria</Section>
<CategoryContainer>
{categories.map((category) => (
<CategoryBox
key={category.key}
onClick={() => {
setFormValues((prev) => ({ ...prev, category: category.key }));
}}
isActive={formValues.category === category.key}
>
<CategoryImage src={category.url} />
{category.label}
</CategoryBox>
))}
</CategoryContainer>
<ButtonContainer>
<Button type="submit">Salvar</Button>
</ButtonContainer>
</Form>
</Container>
);
}