-
Notifications
You must be signed in to change notification settings - Fork 880
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
react-leaflet with react-bootstrap tabs #40
Comments
Hi, thanks! I have never had this use case so I don't know what can be the exact origin, can you setup a jsbin or something showing the issue please? |
Thanks for the quick response. It is a pretty niche problem! I'll try to reproduce it for you somewhere public. |
I'm afraid I don't know how to reproduce it in an environment like jsbin as I don't know how to get react-bootstrap & react-leaflet without npm. The code required for a simple demo is below: index.html
app.js
browserify command
You'll need to npm install The demo has a set up with two tabs. I've put maps in both tabs to show that it loads properly in the default tab but when you switch to the other tab the tiles do not load properly, even when you pan around a bit it seems confused. I'm using Chrome 43 on Ubuntu. I hope that is sufficient but please ask if there are details I've missed. Michael |
Hi, It seems to be a known issue with Leaflet, if its container size is changed or if it's not visible when the map is created. Calling Here is an example to make it work with react-bootstrap: class MapTabs extends React.Component {
constructor() {
super();
this.state = {
key: 1
};
}
onSelect(key) {
this.setState({key});
setTimeout(() => {
this.refs['map' + key].getLeafletElement().invalidateSize(false);
}, 300); // Adjust timeout to tab transition
}
render() {
return (
<TabbedArea activeKey={this.state.key} onSelect={this.onSelect.bind(this)}>
<TabPane eventKey={1} tab='Default Tab'>
<Map ref="map1" center={position} zoom={zoom} style={style}>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<Marker position={position}>
<Popup>
<span>A pretty CSS3 popup.<br/>Easily customizable.</span>
</Popup>
</Marker>
</Map>
</TabPane>
<TabPane eventKey={2} tab='Map Tab'>
<Map ref="map2" center={position} zoom={zoom} style={style}>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<Marker position={position}>
<Popup>
<span>A pretty CSS3 popup.<br/>Easily customizable.</span>
</Popup>
</Marker>
</Map>
</TabPane>
</TabbedArea>
);
}
} The trick is to control the tabs from a parent component, this way you can access the relevant |
Wow, thank you for investigating and for the explaining. Kind of you to give such a clear run down. I will attempt to integrate this approach into my set up. I guess I'll close this ticket as there isn't really an action point for it. It is such an edge case that it doesn't seem worth trying to add to the documentation for react-leaflet itself. Much appreciated! |
it eliminates issue with wrong position of the marker and inappropriate loading of the map elements not sure what is the main reason for this problem... the similar issue is described here PaulLeCam/react-leaflet#40
solution using hooks (and mobx sotre) in FunctionComponent : parent tabs Component
child map Component
|
Delay the call to `invalidateSize()` so the parent has the new dimension. When we trigger the `renderLeafletMap` on `componentDidMount` and `UNSAFE_componentWillReceiveProps` the dimensions of the parent container element are the *old* ones. Found it via: let parent = this.leafletMapNode.parentElement; console.log(parent.clientHeight, parentclientWidth); More info: PaulLeCam/react-leaflet#40
To anyone struggling with the map not rendering properly when using react-leaflet with reactstrap tabs, this is what worked for me.
Found this stackoverflow ANSWER helpful which initially didn't work because the whenReady prop will not let you use the Map instance as described in the answer. Instead, |
Hi,
Thanks for the project. I really appreciate being able to use it for my web app.
I am attempting to have a map inside a tab using react-bootstrap tabs. The tab is not selected by at load but it seems that the react-bootstrap implementation still initialises the tab contents with the Map component. When I do change to the tab with the map, the map tiles have not loaded properly and they seem to struggle to load properly as I pan around the map (only some tiles load.) If I change the application to load that tab initially then to map works perfectly.
The react bootstrap tabs have a fade out & in behaviour as you switch tabs, which might affect things. I'm not sure.
Is this something you are familiar with? Can you offer advice or would you have time to look at it if I provide example set up that shows the issue?
I have some react experience but I'm new to Leaflet and I'm not sure where to begin with figuring this out.
Cheers,
Michael
The text was updated successfully, but these errors were encountered: