In Testing Recipes, I want to change to use root.unmount() and createRoot.
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
unmountComponentAtNode is used on Testing Recipes.
https://reactjs.org/docs/testing-recipes.html
However, unmountComponentAtNode(container) has been changed to root.unmount() in react 18.
It also shows to use createRoot.
https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html
So I replaced it in my code as follows
// before
import { unmountComponentAtNode } from "react-dom";
let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});
// after
import { createRoot } from 'react-dom/client';
let container = null;
let root = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
// cleanup on exiting
act(() => root.unmount());
container.remove();
container = null;
});
root.unmount() is wrapped in act(...) because it gives the following warning:
Warning: An update to Root inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
Is this policy and implementation of mine correct?
If correct I will try to create a pull request.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dalla pagina „Testing Recipes“ all’indirizzo https://reactjs.org/docs/testing-recipes.html e confronta i suoi esempi di cleanup e rendering con la „React 18 upgrade guide“ collegata nell’issue. Esamina l’uso proposto di createRoot, root.unmount() e act(), quindi aggiorna le recipes in modo che riflettano la React 18 testing API e non mostrino più il pattern obsoleto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100