Please make sure to check the following boxes before submitting an issue. Thanks!
GeoJSON data should be re-rendered on the map after data prop change
GeoJSON data rendered only once.
Create custom component with Map and GeoJSON components. Pass prop from custom component to GeoJSON component. Change prop. See that old data still rendered.
ex: https://stackoverflow.com/questions/44155385/rendering-geojson-with-react-leaflet
This is the expected behavior, as documented.
@PaulLeCam Can you suggest how to achieve desired behavior?
You can set an unique key on the <GeoJSON> element every time you want it to be changed. In practice this will cause the existing layer to be removed from Leaflet and a new GeoJSON layer to be created with the provided data.
@PaulLeCam do you have a simple example of how can we implement your suggestion?
Thanks
There is nothing specific about it, it's just React behavior.
Put a different key on an element and it will be updated, in this case <GeoJSON key="whatever" data={geojson} /> for example.
Thanks man. I did something like that. But in my case it is not working. I sent you by twitter a piece of code.
Regards
I don't do support by Twitter, please use StackOverflow if you have questions regarding using the library.
This working behavior is useless. Changing key is just workaround.
I think <GeoJSON /> should act like React.PureComponent.
Yet another workaround.
import React, { Component } from 'react';
import {GeoJSON} from 'react-leaflet';
import axios from 'axios';
import SymfonyRouterContext from './SymfonyRouterContext';
class ServiceGeoJson extends Component {
constructor(props, context) {
super(props, context);
this.state = {
data: []
};
this.geoJsonLayer = React.createRef();
}
componentDidMount() {
const {service, params, zoom, center} = this.props;
axios.post(this.context.generate(service, params), {zoom, center}).then(({data}) => {
this.geoJsonLayer.current.leafletElement.clearLayers().addData(data);
this.setState({data});
});
}
render() {
return <GeoJSON data={this.state.data} ref={this.geoJsonLayer}/>;
}
}
ServiceGeoJson.contextType = SymfonyRouterContext;
export default ServiceGeoJson;
Use ref worked out much smoother than using the component key and stopped the flashing on each update.
I implemented this with the useEffect and useRef hooks below (Note we do not need leafletElement):
const geoJsonLayer = useRef(null);
useEffect(() => {
if (geoJsonLayer.current) {
geoJsonLayer.current.clearLayers().addData(geoData);
}
}, [geoData]);
...
<GeoJSON
ref={geodataRef}
// key={`${datakey}-main`}
data={geoData}
onEachFeature={onEachGeoFeature}
style={featureStyling}
/>
Use ref worked out much smoother than using the component key and stopped the flashing on each update.
I implemented this with the useEffect and useRef hooks below (Note we do not need
leafletElement):const geoJsonLayer = useRef(null); useEffect(() => { if (geoJsonLayer.current) { geoJsonLayer.current.clearLayers().addData(geoData); } }, [geoData]); ... <GeoJSON ref={geodataRef} // key={`${datakey}-main`} data={geoData} onEachFeature={onEachGeoFeature} style={featureStyling} />
Somehow this is not possible in version 3 or above.
Most helpful comment
Yet another workaround.