我想以 mapbox-gl 的形式使用 react-native-mapbox-gl/maps,例如,我尽我所能在 useEffect 而不是组件中初始化 MapView 组件。我可以重写下面的代码是否可以用于反应原生谢谢,
import React, { useEffect, useRef, useState } from "react";
import mapboxgl from "mapbox-gl";
import "mapbox-gl/dist/mapbox-gl.css";
const styles = {
width: "100vw",
height: "calc(100vh - 80px)",
position: "absolute"
};
const MapboxGLMap = () => {
const [map, setMap] = useState(null);
const mapContainer = useRef(null);
useEffect(() => {
mapboxgl.accessToken = process.env.REACT_APP_MAPBOX_KEY;
const initializeMap = ({ setMap, mapContainer }) => {
const map = new mapboxgl.Map({
container: mapContainer.current,
style: "mapbox://styles/mapbox/streets-v11", // stylesheet location
center: [0, 0],
zoom: 5
});
map.on("load", () => {
setMap(map);
map.resize();
});
};
if (!map) initializeMap({ setMap, mapContainer });
}, [map]);
return <div ref={el => (mapContainer.current = el)} style={styles} />;
};
export default MapboxGLMap;