Open galmeida12 opened 6 years ago
I had a similar issue to this - I think it is because the newly created shape is persisted inside the state of the <Map>
itself whereas preloaded shapes exist within the state of our own component. When we get an onCreated
event, we update our state, and then and add a <Polygon>
- a duplicate is therefore created.
A "fix" I used was to edit the key
property of the <Map>
based on if our state contains a shape - this forces a new instance of the component to be made and therefore any internal state is lost. I am using the map to only edit a single shape at a time - so my use-case is more straightforward than yours I think @gpiri - you may have to do something more complex (although you have probably solved this / worked around this by now).
The code looks similar to this - I am storing the shape in this.state.coordinates:
const key = this.state.coordinates ? 'map-populated' : 'map-empty';
return (
<Map {...this.getMapOptions()} key={key}>
{this.getTileLayer()}
<FeatureGroup>
<EditControl
position='topright'
onEdited={::this.handlePathEdited}
onCreated={::this.handlePathCreated}
onDeleted={::this.handlePathDeleted}
draw={this.getDrawOptions()} />
{ this.state.coordinates ?
<Polygon positions={this.state.coordinates} /> : null
}
</FeatureGroup>
</Map>
)
Thanks @madebydavid, this helped a lot.
Possibly more efficient: I found that updating a key
on the FeatureGroup
after onCreated
was sufficient to rerender the shapes.
Same issue with multiple points.
@madebydavid, Your solution is only working for single point (and it's updating map center and bounds when creating the first element).
Did you find a way to solve it @gpiri ?
@alex3165 Can you check this issue? Sounds weird.
@maximesahagian - there was a great suggestion by @gordonwoodhull - try setting the key on the feature group instead?
const key = this.state.coordinates ? 'map-populated' : 'map-empty';
return (
<Map {...this.getMapOptions()}>
{this.getTileLayer()}
<FeatureGroup key={key}>
<EditControl
position='topright'
onEdited={::this.handlePathEdited}
onCreated={::this.handlePathCreated}
onDeleted={::this.handlePathDeleted}
draw={this.getDrawOptions()} />
{ this.state.coordinates ?
<Polygon positions={this.state.coordinates} /> : null
}
</FeatureGroup>
</Map>
)
@maximesahagian, I keep a counter for my FeatureGroup
key and I increment it each time a shape is added. Since we want to remove the temporary shapes and replace them with react components, I think this is minimal and correct.
FWIW I also put keys on my shapes (<Polygon />
etc.); otherwise remove doesn’t seem to work right.
Hello @gordonwoodhull, alright, this fix works on FeatureGroup to only create state draws.
Remove works before creating a new shape, so I need to delete them from the state onDelete, how to get the index(s) deleted to delete them from the state?
Thanks :)
I don't really feel good about leaving this comment (as I don't really want to "scoop" this repo, and I think it is a good lib), but I made a repo (https://github.com/andrewdodd/react-leaflet-draw) to show a possible different implementation that I think solves this issue.
If you run the example-full
locally (i.e. git clone....yarn run example-full) you should see the behaviour you are after. The code in the example-full/edit-control.js
shows managing a set of objects in state; dealing with adding, deleting and editing; and triggering on the state changes of the draw plugin.
Hello @andrewdodd,
I've seen this repo yesterday, but I had some dependencies issues into the EditControlFeatureGroup, I'm sure we can find a way to handle shapes in the state easier..
@maximesahagian cool. As I mention in my repo...it might just be easier to copy and adjust the EditControl from this library to suit your specific needs (that's what I did), rather than using this dep as is.
@madebydavid Thank you so much for your replies. The method with keys helps with this bug. I since stopped working on this project so I haven't tried @gordonwoodhull 's solution of changing the key in the FeatureGroup instead.
@maximesahagian, good point, I am keeping a map from layer._leaflet_id
to my own IDs. I build this map in componentDidMount
/Update
(using Refs on the shapes components).
Kind of horrible but it works so I’m leaving it that way for now. Next time I run into trouble I will definitely check out the @andrewdodd approach.
It’s always a pain dealing with one component system wrapping another one. I’m just happy I haven’t run into any absolute blockers on my React journey...
I've juste installed the @andrewdodd dependency, and it's working well :)
Hi there,
I'm having trouble trying to create new polygons. The way that I'm using this library in conjunction with react-leaflet is that I save all the polygons in the component's state, with some additional info for a pop-up with text, when the area is clicked. I show all those polygons in the respective layer in use, where the is also used.
The unwanted behavior is that upon creating a new shape, it duplicates the new shape, rendering the shape that was saved in the state, and also the shape created with the draw:created.
I could refresh the page and only show the polygons contained in the state but that is plain stupid, or I could leave the new shapes out the state, but that way I'm not able to inject the popup with the text.
I'm trying to remove the polygon that is automatically created, so that only the one that I copy to the state is shown, but with no luck so far.
I'll leave here the code of the component in question, which is still "in construction" and using demo values, for reference.
Any help is much appreciated. Thank you