@@ -3,7 +3,7 @@ title: Hook์ ๊ท์น
33---
44
55<Intro >
6- Hook์ JavaScript ํจ์๋ก ์ ์๋์ง๋ง ํธ์ถ ์์น์ ์ ์ฝ์ด ์๋ ํน๋ณํ ์ ํ์ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ UI ๋ก์ง์
๋๋ค.
6+ Hook์ ์๋ฐ์คํฌ๋ฆฝํธ ํจ์๋ก ์ ์๋์ง๋ง ํธ์ถ ์์น์ ์ ์ฝ์ด ์๋ ํน๋ณํ ์ ํ์ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ UI ๋ก์ง์
๋๋ค.
77</Intro >
88
99<InlineToc />
@@ -12,7 +12,7 @@ Hook์ JavaScript ํจ์๋ก ์ ์๋์ง๋ง ํธ์ถ ์์น์ ์ ์ฝ์ด ์๋
1212
1313## Hook์ ์ต์์ ๋ ๋ฒจ์์๋ง ํธ์ถํ์ธ์ {/* only-call-hooks-at-the-top-level* /}
1414
15- React์์๋ ` use ` ๋ก ์์ํ๋ ํจ์๋ฅผ [ * Hook* ] ( /reference/react ) ์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
15+ React์์๋ ` use ` ๋ก ์์ํ๋ ํจ์๋ฅผ [ * Hook* ] ( /reference/react ) ์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
1616
1717** Hook์ ๋ฐ๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ, ์ค์ฒฉ ํจ์, ๋๋ ` try ` /` catch ` /` finally ` ๋ธ๋ก ๋ด๋ถ์์ ํธ์ถํ์ง ๋ง์ธ์.** ๋์ Hook์ ํญ์ React ํจ์์ ์ต์์ ๋ ๋ฒจ์์ ํธ์ถํ๊ณ , early return ์ด์ ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค. Hook์ React๊ฐ ํจ์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ ๋์์๋ง ํธ์ถํ ์ ์์ต๋๋ค.
1818
@@ -21,13 +21,13 @@ React์์๋ `use`๋ก ์์ํ๋ ํจ์๋ฅผ [*Hook*](/reference/react) ์ด๋ผ
2121
2222``` js{2-3,8-9}
2323function Counter() {
24- // โ
ํจ์ ์ปดํฌ๋ํธ์ ์ต์์ ๋ ๋ฒจ์์ ์ฌ์ฉํฉ๋๋ค
24+ // โ
Good: ํจ์ ์ปดํฌ๋ํธ์ ์ต์์ ๋ ๋ฒจ์์ ์ฌ์ฉํฉ๋๋ค.
2525 const [count, setCount] = useState(0);
2626 // ...
2727}
2828
2929function useWindowWidth() {
30- // โ
์ปค์คํ
Hook์ ์ต์์ ๋ ๋ฒจ์์ ์ฌ์ฉํฉ๋๋ค
30+ // โ
Good: ์ปค์คํ
Hook์ ์ต์์ ๋ ๋ฒจ์์ ์ฌ์ฉํฉ๋๋ค.
3131 const [width, setWidth] = useState(window.innerWidth);
3232 // ...
3333}
@@ -47,15 +47,15 @@ function useWindowWidth() {
4747``` js{3-4,11-12,20-21}
4848function Bad({ cond }) {
4949 if (cond) {
50- // ๐ด ์กฐ๊ฑด๋ถ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
50+ // ๐ด Bad: ์กฐ๊ฑด๋ถ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
5151 const theme = useContext(ThemeContext);
5252 }
5353 // ...
5454}
5555
5656function Bad() {
5757 for (let i = 0; i < 10; i++) {
58- // ๐ด ๋ฐ๋ณต๋ฌธ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
58+ // ๐ด Bad: ๋ฐ๋ณต๋ฌธ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
5959 const theme = useContext(ThemeContext);
6060 }
6161 // ...
@@ -65,22 +65,22 @@ function Bad({ cond }) {
6565 if (cond) {
6666 return;
6767 }
68- // ๐ด ์กฐ๊ฑด๋ถ return๋ฌธ ์ดํ (์์ ํ๋ ค๋ฉด return๋ฌธ ์ด์ ์ผ๋ก ์ด๋ํ์ธ์!)
68+ // ๐ด Bad: ์กฐ๊ฑด๋ถ `return`๋ฌธ ์ดํ (์์ ํ๋ ค๋ฉด `return`๋ฌธ ์ด์ ์ผ๋ก ์ด๋ํ์ธ์!)
6969 const theme = useContext(ThemeContext);
7070 // ...
7171}
7272
7373function Bad() {
7474 function handleClick() {
75- // ๐ด ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
75+ // ๐ด Bad: ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
7676 const theme = useContext(ThemeContext);
7777 }
7878 // ...
7979}
8080
8181function Bad() {
8282 const style = useMemo(() => {
83- // ๐ด useMemo ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
83+ // ๐ด Bad: ` useMemo` ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
8484 const theme = useContext(ThemeContext);
8585 return createStyle(theme);
8686 });
@@ -89,15 +89,15 @@ function Bad() {
8989
9090class Bad extends React.Component {
9191 render() {
92- // ๐ด ํด๋์ค ์ปดํฌ๋ํธ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ํด๋์ค ์ปดํฌ๋ํธ ๋์ ํจ์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ธ์!)
92+ // ๐ด Bad: ํด๋์ค ์ปดํฌ๋ํธ ๋ด๋ถ (์์ ํ๋ ค๋ฉด ํด๋์ค ์ปดํฌ๋ํธ ๋์ ํจ์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ธ์!)
9393 useEffect(() => {})
9494 // ...
9595 }
9696}
9797
9898function Bad() {
9999 try {
100- // ๐ด try/ catch/ finally ๋ธ๋ก ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
100+ // ๐ด Bad: ` try`/` catch`/` finally` ๋ธ๋ก ๋ด๋ถ (์์ ํ๋ ค๋ฉด ์ธ๋ถ๋ก ์ด๋ํ์ธ์!)
101101 const [x, setX] = useState(0);
102102 } catch {
103103 const [x, setX] = useState(1);
@@ -109,15 +109,15 @@ function Bad() {
109109
110110<Note >
111111
112- [ ์ปค์คํ
Hook] ( /learn/reusing-logic-with-custom-hooks ) ์ ๋ค๋ฅธ Hook์ * ํธ์ถํ ์ ์์ต๋๋ค* (๊ทธ๊ฒ์ด ์ปค์คํ
Hook์ ์ฃผ๋ ๋ชฉ์ ์
๋๋ค). ์ปค์คํ
Hook๋ ํจ์ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ ๋์์๋ง ํธ์ถ๋ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
112+ [ ์ปค์คํ
Hook] ( /learn/reusing-logic-with-custom-hooks ) ์ ๋ค๋ฅธ Hook์ * ํธ์ถํ ์ ์์ต๋๋ค* . (์ด๊ฒ์ด ๋ฐ๋ก ์ปค์คํ
Hook์ ์ฃผ๋ ๋ชฉ์ ์
๋๋ค.) ์ปค์คํ
Hook๋ ํจ์ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ ๋์์๋ง ํธ์ถ๋ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
113113
114114</Note >
115115
116116---
117117
118118## Hook์ React ํจ์์์๋ง ํธ์ถํ์ธ์ {/* only-call-hooks-from-react-functions* /}
119119
120- ์ผ๋ฐ JavaScript ํจ์์์ Hook์ ํธ์ถํ์ง ๋ง์ธ์. ๋์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
120+ ์ผ๋ฐ ์๋ฐ์คํฌ๋ฆฝํธ ํจ์์์ Hook์ ํธ์ถํ์ง ๋ง์ธ์. ๋์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
121121
122122โ
Hook์ React ํจ์ ์ปดํฌ๋ํธ์์ ํธ์ถํ์ธ์.
123123โ
Hook์ [ ์ปค์คํ
Hook] ( /learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component ) ์์ ํธ์ถํ์ธ์.
0 commit comments