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

オープン
#2,107 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
react, typescript

調査の方向性

source.js と提供された CodeSandbox の再現から始め、スタックトレースを使って dataloading ハンドラーを updateSource と Source コンポーネントまで追跡します。記載されている React、マップライブラリ、ブラウザー、OS のバージョンで警告を確認します。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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

visgl/react-map-gl のほかの issue

visgl/react-map-gl の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。