mapbox / mapbox/mapbox-gl-js

Rerender map when React state changes

Open
#12,654 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs information :pray:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

**mapbox-gl-js version**: `^2.13.0`

### Question

I have a map that needs to rerender whenever the state of `sensors` changes. I currently have the following code:

```ts
// Custom hook for handling logic related to the map
const useAllSensorsMap = ({ sensors }: AllSensorsMapProps) => {
const container = useRef(null);
const map = useRef(null);

useEffect(() => {
if (!map.current) return;
map.current.triggerRepaint();
}, [sensors]);

// ... All other functionality for the map. This works as intended.

return {
container,
};
```

The map itself is rendered in the following way, and this works:

```ts
const AllSensorsMap = ({ sensors }: AllSensorsMapProps) => {
const { container } = useAllSensorsMap({ sensors });

return (


{!!container && (



)}

);
};
```

**Why does the map not rerender when calling `triggerRepaint()`?**

I saw [this related issue](https://github.com/mapbox/mapbox-gl-js/issues/7893), but couldn't find a solution there.

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 the useAllSensorsMap hook and the Map.triggerRepaint() call shown in the issue, then trace how the sensors dependency changes and how the map instance is created. Reproduce the behavior with the provided AllSensorsMap component and determine whether the map repaints when sensors changes; the issue provides no repository file or test to update.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.