๊ฐœ๋ฐœ ๐Ÿพ/Error

Error] findDOMNode is deprecated in StrictMode

JOTOKKI 2021. 6. 22. 11:05
728x90

1. ์—๋Ÿฌ ๋ฐœ์ƒ ์›์ธ

react-draggable์„ ์“ธ ์ผ์ด ์ƒ๊ฒจ์„œ ์‚ฌ์šฉํ•˜๋˜ ์ค‘,

๋™์ž‘์€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ–ˆ์œผ๋‚˜ ์ฝ˜์†”์— ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค. 

์—๋Ÿฌ ์ด๋ฏธ์ง€

๋ณด๋‹ค์‹œํ”ผ findDOMNode ๋Š” StrictMode์—์„œ ์ œ๊ฑฐ๋˜์—ˆ๋‹ค๋Š” ๋‚ด์šฉ์ด๋‹ค. 

StricMode๋Š” React16์—์„œ ์ถ”๊ฐ€๋œ ๋ชจ๋“œ๋กœ ์ƒ๋ช…์ฃผ๊ธฐ์— ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ณ ์ž ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. 

 

์ฐธ์กฐ : https://ko.reactjs.org/docs/strict-mode.html

 

Strict ๋ชจ๋“œ – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

react-draggable ๊ฐ™์€ ๊ฒฝ์šฐ ์ง์ ‘ ๊ฐœ์ฒด๋ฅผ ์ปจํŠธ๋กคํ•˜๋‹ค ๋ณด๋‹ˆ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค. 

 

2. ํ•ด๊ฒฐ์ฑ…

2-1. StrictMode ํ•ด์ œ ํ•˜๊ธฐ

๊ตณ์ด strictMode๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋‹ค๋ฉด ํ•ด์ œ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. 

// src/index.js

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

// ์ด ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค

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

 

2-2. nodeRef ์ฃผ๊ธฐ ๐Ÿ‘

์‚ฌ์‹ค ์–ด์ง€๊ฐ„ํ•œ ํ”„๋กœ์ ํŠธ๋Š” StrictMode ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ• ํ…Œ๋‹ˆ, ์œ„์˜ ๋ฐฉ๋ฒ•์ด ์ตœ์„ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์—†๋‹ค. 

์ œ์ผ ์ตœ์ ํ™”๋œ ๋ฐฉ๋ฒ•์€ useRef๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

์‚ฌ์‹ค ์ด๊ฒŒ react-draggable ๊ณต์‹ ์‚ฌ์ดํŠธ์—์„œ๋„ ์ œ์‹œํ•œ ๋ฐฉ์•ˆ์ด๋‹ค. 

ํ•ด๊ฒฐ์ฑ… ์ด๋ฏธ์ง€

https://www.npmjs.com/package/react-draggable

 

react-draggable

React draggable component

www.npmjs.com

 

import { useRef } from 'react';

const nodeRef = useRef(null);

<Draggable nodeRef={nodeRef}>
  <div ref={nodeRef} className="box"></div>
</Draggable>

์ด๋ ‡๊ฒŒ useRef๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊น”๋”ํ•˜๊ฒŒ ํ•ด๊ฒฐ๋œ๋‹ค! 

 

 

๋ฐ˜์‘ํ˜•