reactjs / reactjs/react.dev

UseEffect hook cleanup not running when error occurs somewhere in project

Aperta
#1,894 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Il report indica il punto di ingresso del cleanup di useEffect e i file per riprodurre il problema activities.ts, apiMiddleware.ts e QuestionsSelector.tsx; inizia riproducendo la risposta 400 mentre confronti i componenti funzione e classe. Non viene fornito alcun file di test o file target, quindi verifica se il componente viene smontato e definisci come completato l’accertamento del comportamento di cleanup previsto con un test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
frontend, web-dev
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.