๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป
article thumbnail
๋ฐ˜์‘ํ˜•

[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 ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

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

 

๋ฐ˜์‘ํ˜•
profile

๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป

@์šฉ๋‡ฝ

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!