【发布时间】:2021-09-22 21:23:08
【问题描述】:
我正在尝试为我在 React 中使用钩子和 TypeScript 编写的 mapboxgl 地图添加一个源代码。
export default function App() {
const mapContainer = React.useRef<any>(null);
const map = React.useRef<any>(null);
const [lng, setLng] = React.useState(-74.0632);
const [lat, setLat] = React.useState(40.7346);
const [zoom, setZoom] = React.useState(12);
React.useEffect(() => {
if (map.current) return; // initialize map only once
map.current = new mapboxgl.Map({
container: mapContainer.current,
style: 'mapbox://styles/mapbox/streets-v11',
center: [lng, lat],
zoom: zoom
});
map.addSource('property-data', {
type: 'geojson',
data: 'path/to/data.geojson'
});
});
我遇到以下错误:
“MutableRefObject”类型上不存在属性“addSource”
如果没有,我应该使用什么类型?
【问题讨论】:
标签: reactjs typescript mapbox-gl-js