Skip to content

Commit

Permalink
docs: update rules-of-hooks.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lumirlumir committed Jan 3, 2025
1 parent f39068a commit 0da537e
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/content/reference/rules/rules-of-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Hook์˜ ๊ทœ์น™
---

<Intro>
Hook์€ JavaScript ํ•จ์ˆ˜๋กœ ์ •์˜๋˜์ง€๋งŒ ํ˜ธ์ถœ ์œ„์น˜์— ์ œ์•ฝ์ด ์žˆ๋Š” ํŠน๋ณ„ํ•œ ์œ ํ˜•์˜ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ UI ๋กœ์ง์ž…๋‹ˆ๋‹ค.
Hook์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๋กœ ์ •์˜๋˜์ง€๋งŒ ํ˜ธ์ถœ ์œ„์น˜์— ์ œ์•ฝ์ด ์žˆ๋Š” ํŠน๋ณ„ํ•œ ์œ ํ˜•์˜ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ UI ๋กœ์ง์ž…๋‹ˆ๋‹ค.
</Intro>

<InlineToc />
Expand All @@ -12,7 +12,7 @@ Hook์€ JavaScript ํ•จ์ˆ˜๋กœ ์ •์˜๋˜์ง€๋งŒ ํ˜ธ์ถœ ์œ„์น˜์— ์ œ์•ฝ์ด ์žˆ๋Š”

## Hook์„ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ๋งŒ ํ˜ธ์ถœํ•˜์„ธ์š” {/*only-call-hooks-at-the-top-level*/}

React์—์„œ๋Š” `use`๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ [*Hook*](/reference/react) ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.
React์—์„œ๋Š” `use`๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ [*Hook*](/reference/react)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

**Hook์„ ๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ, ์ค‘์ฒฉ ํ•จ์ˆ˜, ๋˜๋Š” `try`/`catch`/`finally` ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ ํ˜ธ์ถœํ•˜์ง€ ๋งˆ์„ธ์š”.** ๋Œ€์‹  Hook์„ ํ•ญ์ƒ React ํ•จ์ˆ˜์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ํ˜ธ์ถœํ•˜๊ณ , early return ์ด์ „์— ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Hook์€ React๊ฐ€ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๋™์•ˆ์—๋งŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Expand All @@ -21,13 +21,13 @@ React์—์„œ๋Š” `use`๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ [*Hook*](/reference/react) ์ด๋ผ

```js{2-3,8-9}
function Counter() {
// โœ… ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค
// โœ… Good: ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
const [count, setCount] = useState(0);
// ...
}
function useWindowWidth() {
// โœ… ์ปค์Šคํ…€ Hook์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค
// โœ… Good: ์ปค์Šคํ…€ Hook์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
const [width, setWidth] = useState(window.innerWidth);
// ...
}
Expand All @@ -47,15 +47,15 @@ function useWindowWidth() {
```js{3-4,11-12,20-21}
function Bad({ cond }) {
if (cond) {
// ๐Ÿ”ด ์กฐ๊ฑด๋ถ€ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: ์กฐ๊ฑด๋ถ€ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
const theme = useContext(ThemeContext);
}
// ...
}
function Bad() {
for (let i = 0; i < 10; i++) {
// ๐Ÿ”ด ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
const theme = useContext(ThemeContext);
}
// ...
Expand All @@ -65,22 +65,22 @@ function Bad({ cond }) {
if (cond) {
return;
}
// ๐Ÿ”ด ์กฐ๊ฑด๋ถ€ return๋ฌธ ์ดํ›„ (์ˆ˜์ •ํ•˜๋ ค๋ฉด return๋ฌธ ์ด์ „์œผ๋กœ ์ด๋™ํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: ์กฐ๊ฑด๋ถ€ `return`๋ฌธ ์ดํ›„ (์ˆ˜์ •ํ•˜๋ ค๋ฉด `return`๋ฌธ ์ด์ „์œผ๋กœ ์ด๋™ํ•˜์„ธ์š”!)
const theme = useContext(ThemeContext);
// ...
}
function Bad() {
function handleClick() {
// ๐Ÿ”ด ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
const theme = useContext(ThemeContext);
}
// ...
}
function Bad() {
const style = useMemo(() => {
// ๐Ÿ”ด useMemo ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: `useMemo` ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
const theme = useContext(ThemeContext);
return createStyle(theme);
});
Expand All @@ -89,15 +89,15 @@ function Bad() {
class Bad extends React.Component {
render() {
// ๐Ÿ”ด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”!)
useEffect(() => {})
// ...
}
}
function Bad() {
try {
// ๐Ÿ”ด try/catch/finally ๋ธ”๋ก ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
// ๐Ÿ”ด Bad: `try`/`catch`/`finally` ๋ธ”๋ก ๋‚ด๋ถ€ (์ˆ˜์ •ํ•˜๋ ค๋ฉด ์™ธ๋ถ€๋กœ ์ด๋™ํ•˜์„ธ์š”!)
const [x, setX] = useState(0);
} catch {
const [x, setX] = useState(1);
Expand All @@ -109,15 +109,15 @@ function Bad() {

<Note>

[์ปค์Šคํ…€ Hook](/learn/reusing-logic-with-custom-hooks)์€ ๋‹ค๋ฅธ Hook์„ *ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค* (๊ทธ๊ฒƒ์ด ์ปค์Šคํ…€ Hook์˜ ์ฃผ๋œ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค). ์ปค์Šคํ…€ Hook๋„ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋˜๋Š” ๋™์•ˆ์—๋งŒ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
[์ปค์Šคํ…€ Hook](/learn/reusing-logic-with-custom-hooks)์€ ๋‹ค๋ฅธ Hook์„ *ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค*. (์ด๊ฒƒ์ด ๋ฐ”๋กœ ์ปค์Šคํ…€ Hook์˜ ์ฃผ๋œ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค.) ์ปค์Šคํ…€ Hook๋„ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋˜๋Š” ๋™์•ˆ์—๋งŒ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

</Note>

---

## Hook์„ React ํ•จ์ˆ˜์—์„œ๋งŒ ํ˜ธ์ถœํ•˜์„ธ์š” {/*only-call-hooks-from-react-functions*/}

์ผ๋ฐ˜ JavaScript ํ•จ์ˆ˜์—์„œ Hook์„ ํ˜ธ์ถœํ•˜์ง€ ๋งˆ์„ธ์š”. ๋Œ€์‹  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ผ๋ฐ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜์—์„œ Hook์„ ํ˜ธ์ถœํ•˜์ง€ ๋งˆ์„ธ์š”. ๋Œ€์‹  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โœ… Hook์„ React ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ˜ธ์ถœํ•˜์„ธ์š”.
โœ… Hook์„ [์ปค์Šคํ…€ Hook](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component)์—์„œ ํ˜ธ์ถœํ•˜์„ธ์š”.
Expand Down

0 comments on commit 0da537e

Please sign in to comment.