
(+) ๐ฃ ํธ๋ฌ๋ธ์ด์ ๋ฐ์!!!
Uncaught ReferenceError: useRef is not defined ์ค๋ฅ
[React] ์ปดํฌ๋ํธ์ ref ๋ฌ๊ธฐ ์์ ์งํ ์ค์ ๊ณ์ ์ฌ์ฉํด์ค๋ App.jsx ์ฝ๋์ ๊ทธ๋๋ก ์์ฑํ๋๋ฐ, ์น๋ทฐ์ ๋๋๋ง ์ ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
Uncaught ReferenceError: useRef is not defined ์ค๋ฅ๋ React์ useRef ํ ์ ์ฌ์ฉํ๊ธฐ ์ํด useRef๋ฅผ ๊ฐ์ ธ์ค์ง ์์์ ๋ ๋ฐ์ํ๋ค.
๐ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด useRef๋ฅผ React๋ก๋ถํฐ ๋ช
์์ ์ผ๋ก ๊ฐ์ ธ์์ผ ํ๋ค.
useRef๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด import React์ ํจ๊ป useRef๋ฅผ ์ถ๊ฐํด์ผ ํจ!
// import React from 'react';
import React, { useRef } from 'react'; // useRef๋ฅผ ๋ช
์์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
import ScrollBox from './features/5_ref_DOM/ScrollBox';
const App = () => {
// return <ScrollBox />;
const scrollBoxRef = useRef(null); // useRef๋ก ref ์์ฑ
return (
<div>
<ScrollBox ref={scrollBoxRef} />
<button onClick={() => scrollBoxRef.current.scrollToBottom()}>
๋งจ ๋ฐ์ผ๋ก
</button>
</div>
);
};
export default App;
์ด๋ฌํ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๋ ค๋ฉด ํจ์ํ์ธ์ง, ํด๋์คํ์ธ์ง ์ ํ์ธํด์ผ ํ ๋ฏ?!
๊ธฐ์กด ํจ์ํ ์ฃผ์์ฒ๋ฆฌํ๊ณ , ์ฑ
์ ๋์จ๋๋ก ํด๋์คํ์ผ๋ก ์์ฑํ๋๋ ์ ๊ตฌํ๋๋ค.
์ ์ด์ ๋ด๊ฐ App.jsx ์ฝ๋์์ ํจ์ํ, ํด๋์คํ์ ๋์น ๊ฒ ์์ธ์ด์๋ค...