Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customized map control will lose when set reuseMaps to true in StaticMap #699

Closed
hijiangtao opened this issue Jan 23, 2019 · 7 comments
Closed
Labels

Comments

@hijiangtao
Copy link
Contributor

Problem description

I found reuseMaps will keep all the things the same but except the added controls. Once, I add a map control when map is loaded (such as a language control A), then I switch to another page, which also use react-map-gl, I found map in the new page will go back to its original control without A.

Reproduce steps

  1. Include component.js in two different pages, such as /pageA and /pageB;
// component.js
import React from 'react';
import { StaticMap } from 'react-map-gl';

const addControlHandler = (event) => {
  event.target.setLayoutProperty('country-label-lg', 'text-field', ['get', 'name_zh']);
}

const Map = (props) => {
  return (
    <StaticMap
          onLoad={addControlHandler}
          reuseMaps
          mapStyle="mapbox://styles/mapbox/dark-v9"
          preventStyleDiffing
          mapboxApiAccessToken={MAPBOX_TOKEN}
        />
  );
}

export default Map;

// pageA.js
import Map from './component.js';
const App = (props) => {
	return (
		<Map />
	)
}
  1. Add a link to pageB in pageA;
  2. Open pageA, and click the link;
  3. You will see the pageB without the added control in pageA;

If I use reuseMaps, the onLoad will never be fired again in the new page, so I can't add control manually, too.

@Pessimistress
Copy link
Collaborator

onLoad is called if an old Map instance is used: https://github.com/uber/react-map-gl/blob/4.0-release/src/mapbox/mapbox.js#L245 although event is undefined. We should pass an event object regardless, though I'm surprised your code does not throw an error.

@hijiangtao
Copy link
Contributor Author

Why not pass Mapbox.savedMap into the target property of event, since user may need to manually operate something if map control will get lost at this situation?

I have a judgement like this in my project, would it save me from the unexpected error?

// code in this issue
const addControlHandler = (event) => {
  event.target.setLayoutProperty('country-label-lg', 'text-field', ['get', 'name_zh']);
}

// actual code in my project
const addControlHandler = (event) => {
  const map = event && event.target;
  if (map) {
	map.setLayoutProperty('country-label-lg', 'text-field', ['get', 'name_zh']);
  }
}

@hijiangtao
Copy link
Contributor Author

And I thought it means currently I can only set reuseMaps to false to forbid such no-update-triggered-at-second-time situation?

@Pessimistress
Copy link
Collaborator

Why not pass Mapbox.savedMap into the target property of event

No reason, already flagged as a bug. I will try to put up a fix this weekend. If this is blocking you you're welcome to open a PR.

@Pessimistress
Copy link
Collaborator

@hijiangtao is #683 essentially reporting the same bug? Can we merge these?

@hijiangtao
Copy link
Contributor Author

hijiangtao commented Jan 26, 2019

@Pessimistress you remind me of that wired behavior. I got some new ideas on how that happens. I would reply in that issue, but I thought these are two different problems (not sure exactly)?

@hijiangtao
Copy link
Contributor Author

Fixed and closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants