My map is implemented using MapLibre GL, but this issue also happens with Google Maps API or Mapbox GL.
I have the following ReactJS component:
import React, { useRef, useEffect } from "react";
import maplibregl from "maplibre-gl";
import map_style from "./style/style.json";
export default function Map() {
const mapContainer = useRef(null);
const map = useRef(null);
useEffect(() => {
if (map.current) return; //stops map from intializing more than once
map.current = new maplibregl.Map({
container: mapContainer.current,
style: map_style,
center: [12, 26],
});
});
return (
<div
ref={mapContainer}
className="my-map"
style={{ height: 500, width: 500 }}
/>
);
}
The above code renders the map, but I do not want a fixed height
and width
. I need to apply different styles depending on parent DOM elements, screen sizes, etc.
Problem is, right now if I assign a width
and height
in my CSS file, it's not being applied to the map. On the other hand, if I remove or change the syntax of style={{ height: 500, width: 500 }}
in my ReactJS component, the map fails to display.
How can I style with CSS in this scenario?