๐Ÿค ์€์ง€log ๐Ÿค

Npm package install์‹œ Invalid hook call ์—๋Ÿฌ ๋ณธ๋ฌธ

๐ŸŒธ Library

Npm package install์‹œ Invalid hook call ์—๋Ÿฌ

Eun_zii 2022. 11. 11. 12:55

 

 

 "Invalid hook call. Hooks can only be called inside the body of a function component"

์ด๋Ÿฌํ•œ ์—๋Ÿฌ๋ฅผ ๋งˆ์ฃผํ•˜์…จ๋‹ค๋ฉด ์—๋Ÿฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ด์œ ์—์„œ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

1. Having a mismatch between the versions of react and react-dom.

(react ๋ฐ react-dom์˜ ๋ฒ„์ „์ด ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

 

โœ… ๋ฒ„์ „์ด ๋™์ผํ•œ์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”.

 

2. Having multiple versions of the react package in the same project.

(๋™์ผํ•œ ํ”„๋กœ์ ํŠธ์— ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ํŒจํ‚ค์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.)

 

โœ… ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ํŒจํ‚ค์ง€๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”.

 

3. Trying to call a component as a function, e.g. App() instead of <App />

(์ปดํฌ๋„ŒํŠธ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, <App /> ๋Œ€์‹ ์— App()์„ ์‚ฌ์šฉ)

 

โœ… ์ •ํ™•ํ•œ ์ปดํฌ๋„ŒํŠธ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”.

 

4. Using hooks inside of classes or a function that isn't a component or a custom hook.

 

function counter({ 
๐Ÿ—ฃ๏ธ ์ด๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
โœ… function Counter({


๐Ÿ—ฃ๏ธ ๋˜ํ•œ ์ด๋ฆ„ ์•ž์— 'use'๊ฐ€ ๋ถ™์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž ์ •์˜ hooks ๋ผ๊ณ  ํ• ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ—ฃ๏ธ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ๋˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ hooks ๋‚ด๋ถ€์—์„œ๋งŒ hooks๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฆ„ ์•ž์— 'use'๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.
โœ… function useCounter({


โœ… React๋Š” ์ด์ œ useCounter๋ฅผ ์‚ฌ์šฉ์ž ์ •์˜ hooks๋กœ ์ธ์‹ํ•˜๋ฏ€๋กœ ๋‚ด๋ถ€์—์„œ hook์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

โœ… function์˜ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

โœ… ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ, ์‚ฌ์šฉ์ž ์ง€์ • hooks ๋ง๊ณ  ๋‹ค๋ฅธ ๊ณณ์—์„œ hook๋ฅผ ์ง€์ •ํ•œ ๊ฒŒ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”.

 

์ € ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” 1๋ฒˆ react์™€ react-dom์˜ ๋ฒ„์ „์ด ์ผ์น˜ํ•˜์ง€ ์•Š์•„์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”† ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

 

ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  react๋ฐ react-domํŒจํ‚ค์ง€์˜ ๋ฒ„์ „์„ ์—…๋ฐ์ดํŠธํ•˜์—ฌ

๋ฒ„์ „์ด ์ผ์น˜ํ•˜๊ณ  ์˜ค๋ž˜๋œ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 

# ๐Ÿ‘‡๏ธ with NPM
npm install react@latest react-dom@latest

# ๐Ÿ‘‡๏ธ ONLY If you use TypeScript
npm install --save-dev @types/react@latest @types/react-dom@latest

# ------------------------------------------------------------------

# ๐Ÿ‘‡๏ธ with YARN
yarn add react@latest react-dom@latest

# ๐Ÿ‘‡๏ธ ONLY If you use TypeScript
yarn add @types/react@latest @types/react-dom@latest --dev

 

โœ… ์˜ค๋ฅ˜๊ฐ€ ๊ณ„์†๋œ๋‹ค๋ฉด node_modules, package-lock.json (package.json ์•„๋‹˜) ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ณ 

npm ์„ค์น˜๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•œ ํ›„ IDE(VSCode)๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

์ด ์˜ค๋ฅ˜๋Š” ์ข…์ข… ๋™์ผํ•œ ํ”„๋กœ์ ํŠธ์—์„œ ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ๋ฐ˜์‘์ด ์žˆ์„ ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

# ๐Ÿ‘‡๏ธ delete node_modules and package-lock.json

rm -rf node_modules
rm -f package-lock.json

# ๐Ÿ‘‡๏ธ clean npm cache

npm cache clean --force

npm install

 

 

 

 

 

728x90