【发布时间】:2023-01-07 02:36:15
【问题描述】:
我的地图是使用 MapLibre GL 实现的,但问题与 Google Maps API 或 Mapbox GL 相同。
我的 NextJS 应用程序中有以下 ReactJS 组件:
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 className="map-canvas">
<div
ref={mapContainer}
className="my-map"
style={{ height: 600, width: 650 }}
/>
</div>
);
}
上面的代码渲染地图没有问题,但我想用 CSS 设置 className="my-map" 的样式。我需要根据屏幕尺寸等应用不同的样式。问题是,当我删除 style={{ height: 500, width: 500 }} 或更改其语法时,地图无法显示。
在这种情况下如何使用 CSS 设置样式?
【问题讨论】:
标签: css reactjs next.js mapbox