ν”„λ‘œκ·Έλž˜λ°/React JS

[React] ReactDOM.render is no longer supported in React 18 였λ₯˜ ν•΄κ²° 방법

μš©λ‡½ 2022. 4. 11. 23:13
λ°˜μ‘ν˜•

[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

 

λ°˜μ‘ν˜•