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

未关闭
#2,107 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
react, typescript

调研方向

从 source.js 和提供的 CodeSandbox 复现开始,然后使用堆栈跟踪,沿着 updateSource 和 Source 组件追踪 dataloading 处理器。使用所列出的 React、地图库、浏览器和操作系统版本确认该警告。当 dataloading 处理器可以更新另一个组件的 state,而不会触发渲染时警告时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

bug
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
主要语言
TypeScript
星标
8.5k
派生
1.3k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

visgl/react-map-gl 的其他 Issue

查看 visgl/react-map-gl 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。