[React] ReactDOM.render is no longer supported in React 18 μ€λ₯ ν΄κ²° λ°©λ²
π λ€μ΄κ°λ©°
React + Typescript νκ²½μμ React Testing libraryμ jestλ₯Ό μ΄μ©ν΄μ ν μ€νΈ μ½λ μμ±μ λν΄μ 곡λΆνλ €λ λμ€μ μ€λ₯κ° λ°μνκ³ , κ²°κ³Όμ μΌλ‘ ν΄κ²°νκΈ° μν΄ ν΄μΌ ν κ²μ λ³ κ±° μλμμ§λ§ νλ€κ²(?) ν΄κ²° λ°©μμ μ°Ύμμ κΈ°λ‘μ ν΄λλ €κ³ νλ€.
1. λ¬Έμ λ°μ
CRA typescriptλ‘ ν¨ν€μ§λ€μ μ€μΉνκ³ λ νμ npm test λͺ λ Ήμ΄λ₯Ό ν΅ν΄μ κΈ°λ³Έμ μΌλ‘ μμ±λμ΄ μλ ν μ€νΈ μ½λλ€μ΄ μ μ€νμ΄ λλμ§ νμΈμ ν΄λ΄€λ€.
νμ§λ§ ReactDOM.render is no longer supported in React 18 μ€λ₯κ° λ°μνλ€.
ν μ€νΈ μ½λ μ체λ PASSκ° λλ©΄μ μ μ€νμ΄ λμμ§λ§ μμ κ°μ μ€λ₯ λ©μμ§κ° λ§€μ° μ κ²½ μ°μλ€.
2. λ°μ μμΈ
ReactDOM.renderλ React 18μμ λ μ΄μ μ§μλμ§ μμ΅λλ€. λμ createRootλ₯Ό μ¬μ©νμΈμ.
μ APIλ‘ μ νν λκΉμ§ μ±μ React 17μ μ€ννλ κ²μ²λΌ μλν©λλ€.
https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html
How to Upgrade to React 18 – React Blog
As we shared in the release post, React 18 introduces features powered by our new concurrent renderer, with a gradual adoption strategy for existing applications. In this post, we will guide you through the steps for upgrading to React 18. Please report an
reactjs.org
곡μ λ¬Έμμ μ νμλ―μ΄ React 18 λ²μ μΌλ‘ μ λ°μ΄νΈλλ©΄μ ReactDOM.renderλ λ μ΄μ μ§μλμ§ μλλ€κ³ νλ€.
3. ν΄κ²° λ°©λ²
3-1 첫 λ²μ§Έ
λ¨Όμ 곡μλ¬Έμμ λμμλ μμ μ½λμ²λΌ index.tsxλ₯Ό μμ ν΄λ΄€λ€.
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
const container = document.getElementById("root");
const root = createRoot(container);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
κ²°κ³Όλ μ€ν¨.
λκ°μ μ€λ₯κ° λ°μνλ€.
react-dom/clientμ typeμ μ°Ύμ μ μλ€κ³ createRootλ₯Ό μ¬μ©ν μ μ€λ₯κ° λ°μνλ€. (μ΄λ κ² νλΌλ©΄μ μ΄μ©λ κ±°μ§)
3-1-1 첫 λ²μ§Έ μλμ ν΄κ²° λ°©μ
리μ‘νΈ κ³΅μ λ¬Έμλ μ½λλ₯Ό React 18λ‘ λ³ννλ λ°©λ²μ μ€λͺ νμ§λ§ TypeScriptμ λν΄ νμ€ν μ νμ΄ μ§μ λ 리μμ€λ₯Ό μ λ°μ΄νΈνλ λ°©λ²μ λν μ§μΉ¨μ μμλ€.
κ·Έλμ @types/react-dom ν¨ν€μ§λ₯Ό μ λ°μ΄νΈν΄μΌ νλ€.
npm install @types/react-dom@17.0.14
μ΄ μ λ°μ΄νΈλ type μ μμλ React 18μ λν μ λ°μ΄νΈλ μ μΈμ΄ ν¬ν¨λλ€.
import React from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
// React 18
const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
μ΄λ κ² ν΄μ€μΌλ‘μ¨ createRootμ typeμ΄ μλ€λ μ€λ₯λ ν΄κ²°λμλ€.
3-2 λ λ²μ§Έ
μ§κΈκΉμ§ ν κ±Έλ‘ λλ μ€ μμλλ° npm testλ₯Ό μ€νν μ ReactDOM.render is no longer supported in React 18 μ€λ₯κ° μ¬μ ν λ°μνλ€.
κΈ°λ³Έμ μΌλ‘ μμ±λμ΄ μλ app.test.tsxμμ render() ν¨μλ₯Ό μ¬μ©ν΄μ λ°μνλ λ¬Έμ μλ€.
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄μλ μ¬μ© μ€μΈ λͺ¨λ React Testing library ν¨ν€μ§μ λ²μ μ μ λ°μ΄νΈν΄μΌ λλ€.
npm install --save-dev @testing-library/react@latest
npm install --save-dev @testing-library/jest-dom@latest
npm install --save-dev @testing-library/user-event@latest
κ° ν¨ν€μ§λ€μ μ΅μ λ²μ μΌλ‘ μ λ°μ΄νΈλ₯Ό νκ³ νμΈν΄ 보면 λͺ¨λ μ€λ₯κ° μμ΄μ§ κ²μ νμΈν μ μλ€.
π λ§λ¬΄λ¦¬
ν΄κ²°νκ³ λ³΄λ λ³ κ±° μλμμ§λ§ λ무 μ½μ§μ ν κ±° κ°μμ λμ κ°μ κ³ ν΅μ κ²ͺλ μ¬λμ΄ μλ€λ©΄ μμ½κ² ν΄κ²°νμΌλ©΄ λ°λΌλ λ§μμ΄λ€. κ·Έλ¦¬κ³ λ€μμ λ μ΄λ° λ¬Έμ κ° λ°μνμ λ μ°Έκ³ ν μ μλλ‘ μ 리λ₯Ό ν΄λ κ²λ μ΄μ μ€ νλμ΄λ€.
μ°Έκ³ :
https://stackoverflow.com/questions/71684417/upgrading-to-react18-and-react-dom18-fails
Upgrading to react@18 and react-dom@18 fails
I have an existing React 17 app that started life as a create-react-app output with the TypeScript option. I wanted to change package.json to include react@18.0.0 and react-dom@18.0.0 and then sta...
stackoverflow.com
https://bobbyhadz.com/blog/react-testing-library-reactdom-render-not-supported
(React Testing library) ReactDOM.render no longer supported in React 18 | bobbyhadz
To solve the react testing library error "ReactDOM.render is no longer supported in React 18", update the version of the react testing library by running `npm i -D @testing-library/react@latest`. Version 13.0.0 of the package adds support for the new `crea
bobbyhadz.com