New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GeoJSON data not rendered after change #332
Comments
This is the expected behavior, as documented. |
@PaulLeCam Can you suggest how to achieve desired behavior? |
You can set an unique |
@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. |
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 |
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 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. |
@guptaji048 Maybe because there's a typo in your quoted code: variable is called Here is my TypeScript component. The only difference is that style settingsare also handled which seem to be removed by the import React, { ReactElement, useEffect, useRef } from "react";
import { GeoJSON, GeoJSONProps } from "react-leaflet";
import { GeoJSON as LeafletGeoJSON } from "leaflet";
/**
* GeoJsonWithUpdates is a wrapper around react-leaflet's GeoJSON component to support data changes
* See https://github.com/PaulLeCam/react-leaflet/issues/332
*
* It accepts the same props like react-leaflet's GeoJSON component.
* However, updates are only support
*/
export default function GeoJsonWithUpdates(props: GeoJSONProps): ReactElement {
const geoJsonLayerRef = useRef<LeafletGeoJSON | null>(null);
useEffect(() => {
const layer = geoJsonLayerRef.current;
if (layer) {
layer.clearLayers().addData(props.data);
// clearLayers() seems to remove the `pathOptions`, `style` and `interactive` prop as well
// Resetting it here
if (props.pathOptions) {
layer.setStyle(props.pathOptions);
} else if (props.style) {
layer.setStyle(props.style);
}
}
}, [props.data, props.pathOptions, props.style]);
return <GeoJSON {...props} ref={geoJsonLayerRef} />;
} FWIW, for anyone using |
The workaround does not seem to work when It would be nice to have a PR and have this issue opened again, as it is a bug indeed. Changing key is a workaround. |
definitely a design bug. it completely breaks with your day-to-day functional React expectations. if the props change, you expect a re-render. as it is now, there's a decent amount of React and Leaflet plumbing you have to know and exploit to fix the lifecycle issues here. |
hey @jnachtigall , thanks so much for this! I'm using this as a component and calling it in
|
@PaulLeCam Are you willing to reopen this issue? |
I have assigned the data (an object) that will change and necessitate a re-render as the key, which should technically work, but it does not. Instead I use an iterator when I encounter this now. Use the hook to increment the number every time you get new data. Seems dumb, but it works. |
I had to change from this:
to this, to get it to work:
|
Please make sure to check the following boxes before submitting an issue. Thanks!
Expected behavior
GeoJSON data should be re-rendered on the map after
data
prop changeActual behavior
GeoJSON data rendered only once.
Steps to reproduce
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
The text was updated successfully, but these errors were encountered: