@@ -78,74 +78,58 @@ Only `mapRef` and `mapboxApiAccessToken` are required.
7878``` js
7979import ' mapbox-gl/dist/mapbox-gl.css'
8080import ' react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
81- import React , { Component } from ' react'
81+ import React , { useState , useRef , useCallback } from ' react'
8282import MapGL from ' react-map-gl'
8383import Geocoder from ' react-map-gl-geocoder'
8484
85- function getAccessToken () {
86- var accessToken = null ;
87-
88- if (typeof window !== ' undefined' && window .location ) {
89- var match = window .location .search .match (/ access_token=([^ &\/ ] * )/ );
90- accessToken = match && match[1 ];
91- }
92-
93- if (! accessToken && typeof process !== ' undefined' ) {
94- // Note: This depends on bundler plugins (e.g. webpack) inmporting environment correctly
95- accessToken = accessToken || process .env .MapboxAccessToken ; // eslint-disable-line
96- }
97-
98- return accessToken || null ;
99- }
100-
10185// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
102- const MAPBOX_TOKEN = getAccessToken ()
103-
104- class Example extends Component {
105- state = {
106- viewport: {
107- latitude: 37.7577 ,
108- longitude: - 122.4376 ,
109- zoom: 8
110- }
111- }
112-
113- mapRef = React .createRef ()
114-
115- handleViewportChange = (viewport ) => {
116- this .setState ({
117- viewport: { ... this .state .viewport , ... viewport }
118- })
119- }
86+ const MAPBOX_TOKEN = ' REPLACE_WITH_YOUR_MAPBOX_TOKEN'
87+
88+ const Example = () => {
89+ const [viewport , setViewport ] = useState ({
90+ latitude: 37.7577 ,
91+ longitude: - 122.4376 ,
92+ zoom: 8
93+ });
94+ const mapRef = useRef ();
95+ const handleViewportChange = useCallback (
96+ (newViewport ) => setViewport (newViewport),
97+ []
98+ );
12099
121100 // if you are happy with Geocoder default settings, you can just use handleViewportChange directly
122- handleGeocoderViewportChange = (viewport ) => {
123- const geocoderDefaultOverrides = { transitionDuration: 1000 }
124-
125- return this .handleViewportChange ({
126- ... viewport,
127- ... geocoderDefaultOverrides
128- })
129- }
130-
131- render () {
132- return (
101+ const handleGeocoderViewportChange = useCallback (
102+ (newViewport ) => {
103+ const geocoderDefaultOverrides = { transitionDuration: 1000 };
104+
105+ return handleViewportChange ({
106+ ... newViewport,
107+ ... geocoderDefaultOverrides
108+ });
109+ },
110+ [handleViewportChange]
111+ );
112+
113+ return (
114+ < div style= {{ height: " 100vh" }}>
133115 < MapGL
134- ref= {this . mapRef }
135- {... this . state . viewport }
116+ ref= {mapRef}
117+ {... viewport}
136118 width= " 100%"
137119 height= " 100%"
138- onViewportChange= {this .handleViewportChange }
139- mapboxApiAccessToken= {MAPBOX_TOKEN }>
120+ onViewportChange= {handleViewportChange}
121+ mapboxApiAccessToken= {MAPBOX_TOKEN }
122+ >
140123 < Geocoder
141- mapRef= {this . mapRef }
142- onViewportChange= {this . handleGeocoderViewportChange }
124+ mapRef= {mapRef}
125+ onViewportChange= {handleGeocoderViewportChange}
143126 mapboxApiAccessToken= {MAPBOX_TOKEN }
127+ position= " top-left"
144128 / >
145129 < / MapGL>
146- )
147- }
148- }
130+ < / div >
131+ );
132+ };
149133
150134export default Example
151135
0 commit comments