In Testing Recipes, I want to change to use root.unmount() and createRoot.
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 11.8k
- Forks
- 7.9k
- Merge moyen
- 1 j 11 h
- PR mergées (30 j)
- 11
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par la page « Testing Recipes » à l’adresse https://reactjs.org/docs/testing-recipes.html et comparez ses exemples de cleanup et de rendering avec le « React 18 upgrade guide » lié dans l’issue. Examinez l’utilisation proposée de createRoot, root.unmount() et act(), puis mettez à jour les recipes afin qu’elles reflètent la React 18 testing API et n’affichent plus le pattern obsolète.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, react
- Domaine
- documentation
- Type d'issue
- Documentation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 38/100