I am trying to figure out why @here/maps-api-for-javascript
is not working in next js and throwing the below error:
import H from "@here/maps-api-for-javascript";
export default H;
^^^^^^
SyntaxError: Unexpected token 'export'
import React from 'react';
import H from "@here/maps-api-for-javascript";
export default class Map extends React.Component {
constructor(props) {
super(props);
// the reference to the container
this.ref = React.createRef();
// reference to the map
this.map = null;
}
componentDidMount() {
if (!this.map) {
// instantiate a platform, default layers and a map as usual
const platform = new H.service.Platform({
apikey: '{YOUR_API_KEY}'
});
const layers = platform.createDefaultLayers();
const map = new H.Map(
this.ref.current,
layers.vector.normal.map,
{
pixelRatio: window.devicePixelRatio,
center: {lat: 0, lng: 0},
zoom: 2,
},
);
this.map = map;
}
}
render() {
return (
<div
style={{ width: '300px', height:'300px' }}
ref={this.ref}
/>
)
}
}
The way to resolve this issue is with a Dynamic Import<\/a> with no SSR. If you follow the Using React<\/a> guide provided by HERE, you will need to import the Map component as follows:
import dynamic from "next/dynamic";
function App() {
const Map = dynamic(() => import("./Map"), {
ssr: false
});
return (
<div>
<Map />
</div>
);
}
export default App;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.