Reputation: 180
I used react-leaflet to visualize a quite long path on a map. Users can select from a list and I would like to have different color for the selected path. Changing the state and rendering again is too slow, I am looking for a faster solution.
Leaflet path elements have setStyle() method, so my first idea was using it instead of rendering again. But how to reference the leaflet layer?
class MyPathComponent extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
if (nextProps.selected){
this.setState({selected: true});
LEAFLET_POLYLINE.setStyle({
color: 'red'
});
}
return false;
}
render() {
return(
<Polyline polylines={this.props.path} />
);
}
}
So what should I write instead of LEAFLET_POLYLINE in this code?
Upvotes: 2
Views: 9241
Reputation: 8924
Full example using React callback ref and adding to @Eric's answer above:
export default class MyMap extends Component {
leafletMap = null;
componentDidMount() {
console.debug(this.leafletMap);
}
setLeafletMapRef = map => (this.leafletMap = map && map.leafletElement);
render() {
return (
<Map
ref={this.setLeafletMapRef}
>
<TileLayer
attribution="Powered by <a href="https://www.esri.com">Esri</a>"
url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
/>
</Map>
);
}
}
Upvotes: 0
Reputation: 23308
Components in react-leaflet
have a property called leafletElement
. I believe you can do something like this:
class MyPathComponent extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
if (nextProps.selected){
this.setState({selected: true});
this.refs.polyline.leafletElement.setStyle({
color: 'red'
});
}
return false;
}
render() {
return(
<Polyline ref="polyline" polylines={this.props.path} />
);
}
}
Two things to note:
leafletElement
is the important part here.Instead of the code above, it may be better to just extend the Polyline
component for your custom component (limited docs here):
import { Polyline } from 'react-leaflet';
class MyPathComponent extends Polyline {
shouldComponentUpdate(nextProps, nextState) {
if (nextProps.selected){
this.setState({selected: true});
this.leafletElement.setStyle({
color: 'red'
});
}
return false;
}
}
Let me know if any of this works out for you.
Upvotes: 9