[英]Rendering new component onclick in React
I'm new to React and am working on a project.我是 React 新手,正在做一个项目。 I'm using mapbox-gl and trying to render a popup whenever a hover event occurs.
我正在使用 mapbox-gl 并尝试在发生 hover 事件时呈现弹出窗口。 Right now, if i log the coordinates and description to console it works properly, but the popup fails to display at all.
现在,如果我将坐标和描述记录到控制台,它可以正常工作,但弹出窗口根本无法显示。 Is there something else I need to do to get the popup component to render when the layer is hovered?
当图层悬停时,我还需要做些什么来渲染弹出组件吗?
popupClick(e: any) {
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.description
e.target._canvas.style.cursor = 'default';
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
return (
<Popup
coordinates={coordinates}
>
{description}
</Popup>
)
}
<Layer
id = 'markers'
style = 'cursor-pointer'
layout={{
'icon-image': '{icon}-15',
'icon-allow-overlap': true
}}
onMouseEnter = {this.popupClick}
onMouseLeave = {this.popupLeave}
sourceId='places'
/>
You can may be set visibility status of popup when your hover event happens.当您的 hover 事件发生时,您可以设置弹出窗口的可见性状态。
popupClick(e: any) {
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.description
e.target._canvas.style.cursor = 'default';
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
this.setState({showPopUp: true})
}
and then in your render()
you can do something like然后在你的
render()
你可以做类似的事情
<Layer
id = 'markers'
style = 'cursor-pointer'
layout={{
'icon-image': '{icon}-15',
'icon-allow-overlap': true
}}
onMouseEnter = {this.popupClick}
onMouseLeave = {this.popupLeave}
sourceId='places'
/>
showPopUp &&
<Popup coordinates={coordinates} >
{description}
</Popup>
so your Popup element will be displayed when showPopUp is true.因此当 showPopUp 为 true 时,将显示您的 Popup 元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.