Cleanup popup nodes

Before, the component instances used for rendering popup content were
kept around, slowly leaking memory. This could be observed using react
developer tools.
This commit is contained in:
Alexander Clausen 2019-01-06 05:49:03 +01:00
parent 84654e81af
commit 8f391d7d52

View file

@ -17,10 +17,12 @@ import '../../libs/mapbox-rtl'
const IS_SUPPORTED = MapboxGl.supported(); const IS_SUPPORTED = MapboxGl.supported();
function renderPropertyPopup(features) { function renderPopup(popup) {
var mountNode = document.createElement('div'); var mountNode = document.createElement('div');
ReactDOM.render(<FeaturePropertyPopup features={features} />, mountNode) ReactDOM.render(popup, mountNode)
return mountNode.innerHTML; var content = mountNode.innerHTML;
ReactDOM.unmountComponentAtNode(mountNode);
return content;
} }
function buildInspectStyle(originalMapStyle, coloredLayers, highlightedLayer) { function buildInspectStyle(originalMapStyle, coloredLayers, highlightedLayer) {
@ -162,11 +164,9 @@ export default class MapboxGlMap extends React.Component {
buildInspectStyle: (originalMapStyle, coloredLayers) => buildInspectStyle(originalMapStyle, coloredLayers, this.props.highlightedLayer), buildInspectStyle: (originalMapStyle, coloredLayers) => buildInspectStyle(originalMapStyle, coloredLayers, this.props.highlightedLayer),
renderPopup: features => { renderPopup: features => {
if(this.props.inspectModeEnabled) { if(this.props.inspectModeEnabled) {
return renderPropertyPopup(features) return renderPopup(<FeaturePropertyPopup features={features} />);
} else { } else {
var mountNode = document.createElement('div'); return renderPopup(<FeatureLayerPopup features={features} onLayerSelect={this.props.onLayerSelect} />);
ReactDOM.render(<FeatureLayerPopup features={features} onLayerSelect={this.props.onLayerSelect} />, mountNode)
return mountNode
} }
} }
}) })