[React] React hook + Material UI error :: findDOMNode is deprecated in StrictMode.

React์™€ Material UI๋ผ๋Š” React UI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋˜ ์ค‘, ์ œ๋ชฉ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

findDOMNode is deprecated in StrictMode.

KOS ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๋ฐœ๊ฒฌํ–ˆ๋˜ ์˜ค๋ฅœ๋ฐ ๋Œ€์ˆ˜๋กญ์ง€ ์•Š๊ฒŒ ๋„˜๊ฒผ๋”๋‹ˆ ์—ฌ๊ธฐ์„œ ๊ฑธ๋ฆฌ์ ๊ฑฐ๋ฆฌ๋Š”๊ตฐ ใ… ใ… 

์–ด์จŒ๋“  ํ•ด๊ฒฐํ•ด๋ณด์ž.

 

์ด์œ 

์ €๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ?

๋จผ์ €, create-react-app์„ ํ†ตํ•ด React ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด, index.js๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๊ฒผ๋‹ค.

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์„œ ๋ณด์•„์•ผํ•  ๊ฒƒ์€ React.StrictMode๋ผ๋Š” tag์ด๋‹ค. ํ•ด๋‹น tag๋Š” React ์ฝ”๋“œ๋ฅผ ์งค ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—ฌ๋Ÿฌ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋Š”๋ฐ, ๊ทธ ์ค‘ ํ•˜๋‚˜๊ฐ€ findDOMnode ๊ฒ€์‚ฌ์ด๋‹ค.

 

ํ•ด๊ฒฐ ๊ณผ์ •

forwardRef

์ด๋Š” ์›น ์„œํ•‘ ๋ช‡ ๋ฒˆ ํ•ด๋ณด๋ฉด React ref๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•ด๊ฒฐ์ฑ…์ด ๋‚˜์˜จ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‹ ๊ธฐํ•œ ๊ฒƒ์€ React ๊ณต์‹ ์‚ฌ์ดํŠธ์— functional component์— ๋Œ€ํ•ด์„œ ref๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ๊ณ  ๋˜์–ด์žˆ๋Š” ๊ฒƒ. ref๋Š” DOM ์š”์†Œ์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๊ณ  class component์˜ ์ธ์Šคํ„ด์Šค์—๋งŒ ์ ์šฉํ•˜๋ผ๊ณ  ํ•œ๋‹ค.

You may not use the ref attribute on function components

https://reactjs.org/docs/refs-and-the-dom.html

 

๊ทธ๋ž˜์„œ, ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ref๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•˜๊ณ  functional component์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ๊ณ  ํ•˜๋ฉด hook์„ ์ด์šฉํ•ด์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋Š” ๋‚œ ์–ด๋–กํ•˜์ง€..?

 

ํ•ด๋‹ต๋„ ์œ„์˜ react ํ™ˆํŽ˜์ด์ง€์— ๋‚˜์™€์žˆ์—ˆ๋‹ค. functional component์—์„œ ref๋ฅผ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด,

  • class component๋กœ ๋ฐ”๊พธ๋˜์ง€
  • forwardRef ๋ฅผ ์“ฐ๋ผ๊ณ  ํ•œ๋‹ค.

 

forwardRef๋Š” ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// App.js
import React, { createRef } from 'react';
import Component from './';

const App = () => {
    const appRef = createRef();

    return(
        <Component ref = {appRef}/>
    );
}

// Component.js
import React, { forwardRef } from 'react';

const Component = forwardRef((props, ref) => {
    return(
        <div ref = {ref}>
            <h1>let's resolve it</h1>
            <div>so tired</div>
        </div>
    );
});

์ด๋ ‡๊ฒŒ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ createRef ํ•จ์ˆ˜๋กœ ref๋ฅผ ๋งŒ๋“  ๋‹ค์Œ, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— props๋กœ ๋„˜๊ฒจ์ค€๋‹ค. ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” forwardRef๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ref๋ฅผ ๋ฐ›๋Š”๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ref๋Š” ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ์ตœ์ƒ์œ„ JSX tag์— ref ์†์„ฑ์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.

 

๋ฌธ์ œ๋Š” ํ•œ ๋ฒˆ๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ซ“์•„๊ฐ€๋ฉด์„œ ๊ณ„์†ํ•ด์„œ ref๋ฅผ ๋งŒ๋“ค๊ณ  ํ• ๋‹นํ•˜๋Š” ๊ฑธ ๋ฐ˜๋ณตํ•ด์•ผํ•œ๋‹ค ใ…‹ใ…‹ํใ… ใ… 

 

๊ทธ๋Ÿฐ๋ฐ...

์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ๋„ findDOMNode ... ์—๋Ÿฌ๋Š” ์—ฌ์ „ํžˆ ๋œฌ๋‹ค. ๊ทธ๋ž˜๋„ ์ค‘๊ฐ„์— ๋“ฑ์žฅํ–ˆ๋˜ ์—๋Ÿฌ๋“ค์€ ref๋กœ ๋‹ค ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.

๋ชจ๋‘ Material UI์˜ ์ž์ฒด tag๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ref๋ฅผ ๋„˜๊ฒจ์ค˜์•ผํ•œ๋‹ค๋Š” ์—๋Ÿฌ๋“ค์ด๋‹ค.

  • ์—๋Ÿฌ ์˜ˆ์‹œ : Invalid prop children supplied to ForwardRef(Slide).

    => Slide tag ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ๋‚˜์˜จ ์˜ค๋ฅ˜

 

unstable_createMuiStrictModeTheme

์•„์ง ์—๋Ÿฌ๊ฐ€ ์‚ฌ๋ผ์ง€์ง€ ์•Š์•˜์œผ๋‹ˆ, ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด์ž.

๋‹ค๋“ค ๋ง‰๋ง‰ํ–ˆ๋Š”์ง€ ์ค‘๊ฐ„์ค‘๊ฐ„์— StrictMode ํƒœ๊ทธ๋ฅผ ๊ทธ๋ƒฅ ์—†์• ๋ฒ„๋ฆฌ๋ผ๋Š” ์กฐ์–ธ์ด ์ข…์ข… ๋‚˜์˜จ๋‹ค...

 

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ MaterialUI์—์„œ ๊ด€๋ จ ๊ธ€์„ ์ž‘์„ฑํ•ด๋‘” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

https://material-ui.com/customization/theming/

 

MaterialUI ์ธก๋„ ๋‹น์—ฐํžˆ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์•Œ๊ณ  ์žˆ์—ˆ๊ณ , ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋Œ€์•ˆ์„ ๋งˆ๋ จํ•ด๋‘์—ˆ๋‹ค. ์ฝ”๋“œ๋กœ ์†Œ๊ฐœํ•˜๊ฒ ๋‹ค.

import { ThemeProvider } from "@material-ui/core/styles";
import { unstable_createMuiStrictModeTheme } from '@material-ui/core/styles';
const theme = unstable_createMuiStrictModeTheme();

const App = () => {
            <ThemeProvider theme = {theme}>
                <Component/>
            </ThemeProvider>
}

์ด๋ ‡๊ฒŒ ์ƒ์œ„ ๊ณ„์ธต ์ปดํฌ๋„ŒํŠธ์—์„œ ThemProvider๋ฅผ importํ•ด์„œ wrapper tag๋กœ ๊ฐ์‹ผ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น tag์˜ ์†์„ฑ theme์— unstable_createMuiStrictModeTheme๋ฅผ import ํ•œ ๋’ค ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ์–ป์€ theme์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ๊นŒ์ง€ ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋œจ์ง€ ์•Š๊ณ  ์ž˜ ๋™์ž‘ํ•œ๋‹ค :)

๋ฐ˜์‘ํ˜•