mapbox / mapbox/mapbox-gl-js

many methods error after `map.setStyle(null)`.

Open
#7,477 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug :lady_beetle:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

**mapbox-gl-js version**: master

**browser**: all

### Steps to Trigger Behavior

1. `map.setStyle(null)`
2. call a method such as `map.getLayer("asdf")`

### Link to Demonstration

https://jsbin.com/jufijesasu/edit?html,output

### Expected Behavior

`Uncaught TypeError: Cannot read property 'getLayer' of undefined`

### Actual Behavior

Returns nothing and *maybe* fires an error event.

---

I'm seeing two possible approaches to fixing this.

A) Never let a user set the style to null (`map.setStyle(null)`).
B) handle `undefined` wherever `map.style` is accessed

Flow highlights all the cases when you make `map.style` optional.

```diff
diff --git a/src/ui/map.js b/src/ui/map.js
index 8ff6957ac..452880d29 100755
--- a/src/ui/map.js
+++ b/src/ui/map.js
@@ -228,7 +228,7 @@ const defaultOptions = {
* @see [Display a map](https://www.mapbox.com/mapbox-gl-js/examples/)
*/
class Map extends Camera {
- style: Style;
+ style: ?Style;
painter: Painter;

_container: HTMLElement;
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/ui/map.js and reproduce the jsbin case by calling map.setStyle(null), then map.getLayer("asdf") and other affected methods. Trace the map.style accesses highlighted by the issue's Flow diff. Done means the behavior after a null style is consistent and the reported methods no longer fail silently or emit an unexpected error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.