[Bug] "Cannot update a component while rendering a different component" error in `dataloading` event handler
Nobody has claimed this yet.
- 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 insideSource, 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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