I've got problem about how export nested const 'mapObj' in below example, because i need this in another component to get some information from this object. When I want to export nested const then I've got error that you can export only on the top.
First component:
const Mapper = ({
componentId,
width,
height,
xRenderCoord,
yRenderCoord,
zoom,
projection,
markerLayers = new TileLayer({
source: new OSM()
})
}) => {
const initMap = () => {
const mapObj = new Map({
controls: controls,
target: componentId === '' ? 'map' : componentId,
layers: [
new TileLayer({
source: new OSM()
}),
markerLayers
],
view: new View({
projection: projection,
center: fromLonLat([xRenderCoord, yRenderCoord]),
zoom: zoom
})
})
}
useEffect(() => {
initMap()
}, [])
return (
<React.Fragment>
<div
id={componentId === '' ? 'map' : componentId}
style={{
width: width,
height: height
}}
/>
</React.Fragment>
)
}
export default Mapper
second component:
const MapService = (props) => {
async function vectorLoader(data) {
var geoJsonFormat = new GeoJSON();
var features = geoJsonFormat.readFeatures(data, {
featureProjection: mapObj.getView().getProjection()
})
}
return (
<>
<Mapper
componentId='map'
width='800px'
height='800px'
xRenderCoord={19}
yRenderCoord={52}
zoom={6}
projection='EPSG:3857'
/>
</>
)
}
Do you have any idea to solve this problem?