const IconGen = ({ incomingData }) => {
const [dataMap, setDataMap] = useState({});
const dataMapTemp = {};
incomingData.BandData.forEach((each) => {
dataMapTemp[each.AntennaX].list.push(each);
dataMapTemp[each.AntennaX].angle= each.angle;
dataMapTemp[each.AntennaX].sId = each.SID;
});
if (!dataMap[1]) {
setDataMap(dataMapTemp);
}
return (
<div class="container_Sector" >
<div class="circle" style={{backgroundColor : "#c31605"></div>
<div
id = {dataMap && dataMap[1]?.sId }
style={{
rotate: `${dataMap[1]?.angle}deg`,
}}
onClick={() => {
alert("You clicked the sector!");
}
}
>
{dataMap[1]?.list.map((each) => generateSvg(each))}
</div>
<div
style={{
rotate: `${dataMap[2]?.angle}deg`,
}}
>
{dataMap[2]?.list.map((each) => generateSvg(each))}
</div>
<div
style={{
rotate: `${dataMap[3]?.angle}deg`,
}}
>
{dataMap[3]?.list.map((each) => generateSvg(each))}
</div>
</div>
);
};
export default IconGen;
//Parent Component
<MapContainer>
<Marker
key={data.SiteID}
position={[data.Latitude, data.Longitude]}
icon = <IconGen
incomingData={data}
/>
>
</Marker>
</Mapcontainer>
I am able to render custom icon using icon={L.divIcon({ className: "custom icon", html: ReactDOMServer.renderToString( <MyComponent/> ) })}
.
However the onClick
within the custom icon component does not trigger. onClick
is not working due to rendering the MyComponent using ReactDOMServer.renderToString
.
I need the onClick
event inside the custom component to function correctly.
ReactDOMServer.renderToString()
? What is the variableL
? If you render to string the react runtime is not controlling that component and so any interactions defined within that component will not work, as you are seeing. – HandgunrenderToString()
trick (which ` react-leaflet-enhanced-marker` also uses under the hood) will allow for a HTML string to be produced from a react component -- but crucially, it will not be a fully interactible component. The react runtime is not managing that content or even aware of it beyond the initial call torenderToString
. However, please share the code of this icon. It may be possible to workaround this. – Handgun