reactjs / reactjs/react.dev

UseEffect hook cleanup not running when error occurs somewhere in project

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

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

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

I have the following code in my component;

useEffect(() => {
    console.log("hi");

    return () => {
      console.log("Goodbye")
    }
  }, []);

When the page with said component opens hi is logged and whenever I leave said page Goodbye is logged.

That is, unless somewhere else in the app an error occurs, then the following gets logged;

hi
xhr.js:173 GET http://holiday-assistant.localhost/api/activities/search?answers[]=1&answers[]=3 400 (Bad Request)
dispatchXhrRequest @ xhr.js:173
xhrAdapter @ xhr.js:18
dispatchRequest @ dispatchRequest.js:49
Promise.then (async)
request @ Axios.js:55
Axios.(anonymous function) @ Axios.js:65
wrap @ bind.js:11
getActivities @ activities.ts:11
(anonymous) @ apiMiddleware.ts:66
handleSubmit @ QuestionsSelector.tsx:50
callCallback @ react-dom.development.js:147
invokeGuardedCallbackDev @ react-dom.development.js:196
invokeGuardedCallback @ react-dom.development.js:250
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:265
executeDispatch @ react-dom.development.js:571
executeDispatchesInOrder @ react-dom.development.js:596
executeDispatchesAndRelease @ react-dom.development.js:695
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:704
forEachAccumulated @ react-dom.development.js:676
runEventsInBatch @ react-dom.development.js:844
runExtractedEventsInBatch @ react-dom.development.js:852
handleTopLevel @ react-dom.development.js:5030
batchedUpdates$1 @ react-dom.development.js:21437
batchedUpdates @ react-dom.development.js:2247
dispatchEvent @ react-dom.development.js:5110
(anonymous) @ react-dom.development.js:21494
unstable_runWithPriority @ scheduler.development.js:255
interactiveUpdates$1 @ react-dom.development.js:21493
interactiveUpdates @ react-dom.development.js:2268
dispatchInteractiveEvent @ react-dom.development.js:5086

As you can see an error has occurred and is logged properly, however Goodbye is not logged anymore.

When looking in the React debugger the component is no longer there.

Recreating the component as a class component like so;

class ActivityList extends React.Component {
  componentWillUnmount(): void {
    console.log("Goodbye");
  }

  render() {
    return <p>hi</p>
  }
}

does work; Goodbye is logged even though the above error has occurred.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

レポートでは、useEffect のクリーンアップのエントリポイントと、再現用ファイル activities.ts、apiMiddleware.ts、QuestionsSelector.tsx が示されています。まず、関数コンポーネントとクラスコンポーネントを比較しながら 400 レスポンスを再現してください。テストファイルも対象ファイルも指定されていないため、コンポーネントがアンマウントされるかを確認し、回帰テストによって期待されるクリーンアップ動作を確立することを完了条件としてください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
frontend, web-dev
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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