【发布时间】:2021-07-20 19:38:52
【问题描述】:
下面是我从 Maps API for JavaScript“使用 React”部分重构的代码。我是 React 新手,在学校学习,我们必须使用函数式组件。 HERE 提供了类组件语法的指导,所以我希望这是一个翻译错误
import logo from './logo.svg';
import './App.css';
import React, {useState, useEffect} from 'react';
import H from "@here/maps-api-for-javascript";
import {maps} from "./Settings"
export const Map = () => {
const [map, setMap] = useState()
// // the reference to the container
// const ref = React.createRef();
// // reference to the map
// const map = null;
useEffect(() => {
if (!map) {
// instantiate a platform, default layers and a map as usual
const platform = new H.service.Platform({
apikey: `${maps.apiKey}`
});
const layers = platform.createDefaultLayers();
debugger
const newMap = new H.Map(
layers.vector.normal.map,
{
pixelRatio: window.devicePixelRatio,
center: {lat: 0, lng: 0},
zoom: 2,
},
);
setMap(newMap)
}
}, [])
return (
<div
style={{ width: '300px', height:'300px' }}
{...map}> </div>
)
}
【问题讨论】:
标签: javascript here-api