visgl / visgl/react-map-gl

[Bug] "Cannot update a component while rendering a different component" error in `dataloading` event handler

Open
#2,107 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
8.5k
Forks
1.4k
Avg merge
5d 17h
Merged PRs (30d)
3

Description

Description

When there is an event handler hooked to mapRef's dataloading event
and this handler calls setState (say, for enabling spinning wheel feedback),
the following "Warning"-level exception is dumped:

Warning: Cannot update a component (MyMap) while rendering a different component (Source). To locate the bad setState() call inside Source, follow the stack trace as described in https://reactjs.org/link/setstate-in-render

This seems to be caused by component calling updateSource during rendering.
Maybe this should be an in-effect code instead.

Expected Behavior

Event handlers should allow updating state of other components.

Steps to Reproduce

Minimal example provided here. See "Console" tab for the actual error.

https://codesandbox.io/s/optimistic-monad-dyt59s?file=/src/index.js

Environment
  • Framework version: 7.0.20
  • Map library: 2.4.0
  • Browser: Firefox
  • OS: Linux
Logs

Relevant stack trace fragment below.

    setTrue useBoolean.js:4   (*** state setter ***)
    dataloading (*** my event handler ***)
    fire maplibre-gl.js:535
    _fireEvent mapbox.js:488
    fire maplibre-gl.js:539
    fire maplibre-gl.js:539
    fire maplibre-gl.js:539
    _updateWorkerData maplibre-gl.js:15287
    setData maplibre-gl.js:15262
    updateSource source.js:39
    Source source.js:102

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 source.js and the provided CodeSandbox reproduction, then trace the dataloading handler through updateSource and the Source component using the stack trace. Confirm the warning with the listed React, map library, browser, and OS versions. Done means dataloading handlers can update another component's state without the render-time warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
data-visualization, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.