Skip to content

Commit 42e5dd5

Browse files
committed
Add map instance injection
1 parent 4926e49 commit 42e5dd5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/map.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export interface FactoryParameters {
100100
injectCSS?: boolean;
101101
transformRequest?: RequestTransformFunction;
102102
antialias?: boolean;
103+
mapInstance?: MapboxGl.Map;
103104
}
104105

105106
// Satisfy typescript pitfall with defaultProps
@@ -143,6 +144,7 @@ const ReactMapboxFactory = ({
143144
bearingSnap = 7,
144145
injectCSS = true,
145146
antialias = false,
147+
mapInstance,
146148
transformRequest
147149
}: FactoryParameters) => {
148150
if (injectCSS) {
@@ -165,7 +167,7 @@ const ReactMapboxFactory = ({
165167
};
166168

167169
public state: State = {
168-
map: undefined,
170+
map: mapInstance,
169171
ready: false
170172
};
171173

@@ -261,8 +263,15 @@ const ReactMapboxFactory = ({
261263
opts.pitch = pitch[0];
262264
}
263265

264-
const map = new MapboxGl.Map(opts);
265-
this.setState({ map });
266+
// This is a hack to allow injecting the map instance, which assists
267+
// in testing and theoretically provides a means for users to inject
268+
// their own map instance.
269+
let map = this.state.map;
270+
271+
if (!map) {
272+
map = new MapboxGl.Map(opts);
273+
this.setState({ map });
274+
}
266275

267276
if (fitBounds) {
268277
map.fitBounds(fitBounds, fitBoundsOptions, { fitboundUpdate: true });
@@ -275,7 +284,7 @@ const ReactMapboxFactory = ({
275284
}
276285

277286
if (onStyleLoad) {
278-
onStyleLoad(map, evt);
287+
onStyleLoad(map!, evt);
279288
}
280289
});
281290

0 commit comments

Comments
 (0)