1
1
---
2
2
id : hooks-rules
3
- title : Rules of Hooks
3
+ title : Hook์ ๊ท์น
4
4
permalink : docs/hooks-rules.html
5
5
next : hooks-custom.html
6
6
prev : hooks-effect.html
7
7
---
8
8
9
- * Hooks * are a new addition in React 16.8. They let you use state and other React features without writing a class .
9
+ * Hook * ์ React 16.8์ ์๋ก ์ถ๊ฐ๋ ๊ธฐ๋ฅ์
๋๋ค. Hook์ class๋ฅผ ์์ฑํ์ง ์๊ณ ๋ state์ ๋ค๋ฅธ React์ ๊ธฐ๋ฅ๋ค์ ์ฌ์ฉํ ์ ์๋๋ก ํด์ค๋๋ค .
10
10
11
- Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a [ linter plugin ] ( https://www.npmjs.com/package/eslint-plugin-react-hooks ) to enforce these rules automatically:
11
+ Hook์ JavaScript ํจ์์
๋๋ค. ํ์ง๋ง Hook์ ์ฌ์ฉํ ๋๋ ๋ ๊ฐ์ง ๊ท์น์ ์ค์ํด์ผ ํฉ๋๋ค. ์ฐ๋ฆฌ๋ ์ด๋ฌํ ๊ท์น๋ค์ ์๋์ผ๋ก ๊ฐ์ ํ๊ธฐ ์ํ [ linter ํ๋ฌ๊ทธ์ธ ] ( https://www.npmjs.com/package/eslint-plugin-react-hooks ) ์ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
12
12
13
- ### Only Call Hooks at the Top Level {#only-call-hooks-at-the-top-level}
13
+ ### ์ต์์( at the Top Level)์์๋ง Hook์ ํธ์ถํด์ผ ํฉ๋๋ค {#only-call-hooks-at-the-top-level}
14
14
15
- ** Don't call Hooks inside loops, conditions, or nested functions .** Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That's what allows React to correctly preserve the state of Hooks between multiple ` useState ` and ` useEffect ` calls. (If you're curious, we'll explain this in depth [ below ] ( #explanation ) .)
15
+ ** ๋ฐ๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ ํน์ ์ค์ฒฉ๋ ํจ์ ๋ด์์ Hook์ ํธ์ถํ์ง ๋ง์ธ์ .** ๋์ ํญ์ React ํจ์์ ์ต์์( at the top level)์์ Hook์ ํธ์ถํด์ผ ํฉ๋๋ค. ์ด ๊ท์น์ ๋ฐ๋ฅด๋ฉด ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง ๋ ๋๋ง๋ค ํญ์ ๋์ผํ ์์๋ก Hook์ด ํธ์ถ๋๋ ๊ฒ์ด ๋ณด์ฅ๋ฉ๋๋ค. ์ด๋ฌํ ์ ์ React๊ฐ ` useState ` ์ ` useEffect ` ๊ฐ ์ฌ๋ฌ ๋ฒ ํธ์ถ๋๋ ์ค์๋ Hook์ ์ํ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์งํ ์ ์๋๋ก ํด์ค๋๋ค. ์ด ์ ์ ๋ํด์ ๊ถ๊ธํ๋ค๋ฉด [ ์๋ ] ( #explanation ) ์์ ์์ธํ ์ค๋ช
ํด ๋๋ฆฌ๊ฒ ์ต๋๋ค.
16
16
17
- ### Only Call Hooks from React Functions {#only-call-hooks-from-react-functions}
17
+ ### ์ค์ง React ํจ์ ๋ด์์ Hook์ ํธ์ถํด์ผ ํฉ๋๋ค {#only-call-hooks-from-react-functions}
18
18
19
- ** Don't call Hooks from regular JavaScript functions .** Instead, you can:
19
+ ** Hook์ ์ผ๋ฐ์ ์ธ JavaScript ํจ์์์ ํธ์ถํ์ง ๋ง์ธ์ .** ๋์ ์๋์ ๊ฐ์ด ํธ์ถํ ์ ์์ต๋๋ค.
20
20
21
- * โ
Call Hooks from React function components .
22
- * โ
Call Hooks from custom Hooks (we'll learn about them [ on the next page ] ( /docs/hooks-custom.html ) ).
21
+ * โ
React ํจ์ ์ปดํฌ๋ํธ์์ Hook์ ํธ์ถํ์ธ์ .
22
+ * โ
Custom Hook์์ Hook์ ํธ์ถํ์ธ์. ( [ ๋ค์ ํ์ด์ง ] ( /docs/hooks-custom.html ) ์์ ์ด ๋ถ๋ถ์ ์ดํด๋ณผ ์์ ์
๋๋ค)
23
23
24
- By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code .
24
+ ์ด ๊ท์น์ ์งํค๋ฉด ์ปดํฌ๋ํธ์ ๋ชจ๋ ์ํ ๊ด๋ จ ๋ก์ง์ ์์ค์ฝ๋์์ ๋ช
ํํ๊ฒ ๋ณด์ด๋๋ก ํ ์ ์์ต๋๋ค .
25
25
26
- ## ESLint Plugin {#eslint-plugin}
26
+ ## ESLint ํ๋ฌ๊ทธ์ธ {#eslint-plugin}
27
27
28
- We released an ESLint plugin called [ ` eslint-plugin-react-hooks ` ] ( https://www.npmjs.com/package/eslint-plugin-react-hooks ) that enforces these two rules. You can add this plugin to your project if you'd like to try it:
28
+ ์ฐ๋ฆฌ๋ ์ด ๋ ๊ฐ์ง ๊ท์น์ ๊ฐ์ ํ๋ [ ` eslint-plugin-react-hooks ` ] ( https://www.npmjs.com/package/eslint-plugin-react-hooks ) ๋ผ๋ ESLint ํ๋ฌ๊ทธ์ธ์ ์ถ์ํ์ต๋๋ค. ์ด ํ๋ฌ๊ทธ์ธ์ ํ๋ก์ ํธ์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
29
29
30
30
``` bash
31
31
npm install eslint-plugin-react-hooks
32
32
```
33
33
34
34
``` js
35
- // Your ESLint configuration
35
+ // ESLint ์ค์ ํ์ผ
36
36
{
37
37
" plugins" : [
38
38
// ...
@@ -46,28 +46,28 @@ npm install eslint-plugin-react-hooks
46
46
}
47
47
```
48
48
49
- In the future, we intend to include this plugin by default into Create React App and similar toolkits .
49
+ ์์ผ๋ก๋ Create React App์ด๋ ๋ค๋ฅธ ๋น์ทํ ๊ฐ๋ฐ ๋๊ตฌ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ด ํ๋ฌ๊ทธ์ธ์ ํฌํจํ ์์ ์
๋๋ค .
50
50
51
- ** You can skip to the next page explaining how to write [ your own Hooks ] ( /docs/hooks-custom.html ) now .** On this page, we'll continue by explaining the reasoning behind these rules .
51
+ ** ์ด๋ป๊ฒ [ ๋๋ง์ Hook ] ( /docs/hooks-custom.html ) ์ ์์ฑํ ์ ์๋์ง ์ค๋ช
ํ๋ ๋ค์ ์ฅ์ผ๋ก ์ง๊ธ ๋์ด๊ฐ๋ ์ข์ต๋๋ค .** ์ด๋ฒ ์ฅ์์๋ ๊ณ์ํด์ ์ด๋ฌํ ๊ท์น๋ค์ ๋
ผ๋ฆฌ์ ๊ทผ๊ฑฐ์ ๋ํด ์ค๋ช
ํ ์์ ์
๋๋ค .
52
52
53
- ## Explanation {#explanation}
53
+ ## ์ค๋ช
{#explanation}
54
54
55
- As we [ learned earlier ] ( /docs/hooks-state.html#tip-using-multiple-state-variables ) , we can use multiple State or Effect Hooks in a single component:
55
+ [ ์ด์ ์ ๋ฐฐ์ ๋ฏ์ด ] ( /docs/hooks-state.html#tip-using-multiple-state-variables ) ํ ์ปดํฌ๋ํธ์์ State๋ Effect Hook์ ์ฌ๋ฌ ๊ฐ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
56
56
57
57
``` js
58
58
function Form () {
59
- // 1. Use the name state variable
59
+ // 1. name์ด๋ผ๋ state ๋ณ์๋ฅผ ์ฌ์ฉํ์ธ์.
60
60
const [name , setName ] = useState (' Mary' );
61
61
62
- // 2. Use an effect for persisting the form
62
+ // 2. Effect๋ฅผ ์ฌ์ฉํด ํผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ์ธ์.
63
63
useEffect (function persistForm () {
64
64
localStorage .setItem (' formData' , name);
65
65
});
66
66
67
- // 3. Use the surname state variable
67
+ // 3. surname์ด๋ผ๋ state ๋ณ์๋ฅผ ์ฌ์ฉํ์ธ์.
68
68
const [surname , setSurname ] = useState (' Poppins' );
69
69
70
- // 4. Use an effect for updating the title
70
+ // 4. Effect๋ฅผ ์ฌ์ฉํด์ ์ ๋ชฉ์ ์
๋ฐ์ดํธํฉ๋๋ค.
71
71
useEffect (function updateTitle () {
72
72
document .title = name + ' ' + surname;
73
73
});
@@ -76,63 +76,63 @@ function Form() {
76
76
}
77
77
```
78
78
79
- So how does React know which state corresponds to which ` useState ` call? The answer is that ** React relies on the order in which Hooks are called ** . Our example works because the order of the Hook calls is the same on every render:
79
+ ๊ทธ๋ ๋ค๋ฉด React๋ ์ด๋ป๊ฒ ํน์ state๊ฐ ์ด๋ค ` useState ` ํธ์ถ์ ํด๋นํ๋์ง ์ ์ ์์๊น์? ** ์ ๋ต์ React๊ฐ Hook์ด ํธ์ถ๋๋ ์์์ ์์กดํ๋ค๋ ๊ฒ์
๋๋ค. ** ๋ชจ๋ ๋ ๋๋ง์์ Hook์ ํธ์ถ ์์๋ ๊ฐ๊ธฐ ๋๋ฌธ์ ์์๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ ์ ์์ต๋๋ค.
80
80
81
81
``` js
82
82
// ------------
83
- // First render
83
+ // ์ฒซ ๋ฒ์งธ ๋ ๋๋ง
84
84
// ------------
85
- useState (' Mary' ) // 1. Initialize the name state variable with 'Mary'
86
- useEffect (persistForm) // 2. Add an effect for persisting the form
87
- useState (' Poppins' ) // 3. Initialize the surname state variable with 'Poppins'
88
- useEffect (updateTitle) // 4. Add an effect for updating the title
85
+ useState (' Mary' ) // 1. 'Mary'๋ผ๋ name state ๋ณ์๋ฅผ ์ ์ธํฉ๋๋ค.
86
+ useEffect (persistForm) // 2. ํผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ effect๋ฅผ ์ถ๊ฐํฉ๋๋ค.
87
+ useState (' Poppins' ) // 3. 'Poppins'๋ผ๋ surname state ๋ณ์๋ฅผ ์ ์ธํฉ๋๋ค.
88
+ useEffect (updateTitle) // 4. ์ ๋ชฉ์ ์
๋ฐ์ดํธํ๊ธฐ ์ํ effect๋ฅผ ์ถ๊ฐํฉ๋๋ค.
89
89
90
90
// -------------
91
- // Second render
91
+ // ๋ ๋ฒ์งธ ๋ ๋๋ง
92
92
// -------------
93
- useState (' Mary' ) // 1. Read the name state variable (argument is ignored )
94
- useEffect (persistForm) // 2. Replace the effect for persisting the form
95
- useState (' Poppins' ) // 3. Read the surname state variable (argument is ignored )
96
- useEffect (updateTitle) // 4. Replace the effect for updating the title
93
+ useState (' Mary' ) // 1. name state ๋ณ์๋ฅผ ์ฝ์ต๋๋ค.(์ธ์๋ ๋ฌด์๋ฉ๋๋ค )
94
+ useEffect (persistForm) // 2. ํผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ effect๊ฐ ๋์ฒด๋ฉ๋๋ค.
95
+ useState (' Poppins' ) // 3. surname state ๋ณ์๋ฅผ ์ฝ์ต๋๋ค.(์ธ์๋ ๋ฌด์๋ฉ๋๋ค )
96
+ useEffect (updateTitle) // 4. ์ ๋ชฉ์ ์
๋ฐ์ดํธํ๊ธฐ ์ํ effect๊ฐ ๋์ฒด๋ฉ๋๋ค.
97
97
98
98
// ...
99
99
```
100
100
101
- As long as the order of the Hook calls is the same between renders, React can associate some local state with each of them. But what happens if we put a Hook call (for example, the ` persistForm ` effect) inside a condition ?
101
+ Hook์ ํธ์ถ ์์๊ฐ ๋ ๋๋ง ๊ฐ์ ๋์ผํ๋ค๋ฉด React๋ ์ง์ญ์ ์ธ state๋ฅผ ๊ฐ Hook์ ์ฐ๋์ํฌ ์ ์์ต๋๋ค. ํ์ง๋ง ๋ง์ฝ์ Hook์ ์กฐ๊ฑด๋ฌธ ์์์(์๋ฅผ ๋ค์ด ` persistForm ` effect) ํธ์ถํ๋ค๋ฉด ์ด๋ค ์ผ์ด ์ผ์ด๋ ๊น์ ?
102
102
103
103
``` js
104
- // ๐ด We're breaking the first rule by using a Hook in a condition
104
+ // ๐ด ์กฐ๊ฑด๋ฌธ์ Hook์ ์ฌ์ฉํจ์ผ๋ก์จ ์ฒซ ๋ฒ์งธ ๊ท์น์ ๊นผ์ต๋๋ค
105
105
if (name !== ' ' ) {
106
106
useEffect (function persistForm () {
107
107
localStorage .setItem (' formData' , name);
108
108
});
109
109
}
110
110
```
111
111
112
- The ` name !== '' ` condition is ` true ` on the first render, so we run this Hook. However, on the next render the user might clear the form, making the condition ` false ` . Now that we skip this Hook during rendering, the order of the Hook calls becomes different:
112
+ ` name !== '' ` ์กฐ๊ฑด์ ์ฒซ ๋ฒ์งธ ๋ ๋๋ง์์ ` true ` ๊ธฐ ๋๋ฌธ์ Hook์ ๋์ํฉ๋๋ค. ํ์ง๋ง ์ฌ์ฉ์๊ฐ ๊ทธ๋ค์ ๋ ๋๋ง์์ ํผ์ ์ด๊ธฐํํ๋ฉด์ ์กฐ๊ฑด์ ` false ` ๋ก ๋ง๋ค ๊ฒ๋๋ค. ๋ ๋๋ง ๊ฐ์ Hook์ ๊ฑด๋๋ฐ๊ธฐ ๋๋ฌธ์ Hook ํธ์ถ ์์๋ ๋ฌ๋ผ์ง๊ฒ ๋ฉ๋๋ค.
113
113
114
114
``` js
115
- useState (' Mary' ) // 1. Read the name state variable (argument is ignored )
116
- // useEffect(persistForm) // ๐ด This Hook was skipped !
117
- useState (' Poppins' ) // ๐ด 2 (but was 3 ). Fail to read the surname state variable
118
- useEffect (updateTitle) // ๐ด 3 (but was 4 ). Fail to replace the effect
115
+ useState (' Mary' ) // 1. name state ๋ณ์๋ฅผ ์ฝ์ต๋๋ค. (์ธ์๋ ๋ฌด์๋ฉ๋๋ค )
116
+ // useEffect(persistForm) // ๐ด Hook์ ๊ฑด๋๋ฐ์์ต๋๋ค !
117
+ useState (' Poppins' ) // ๐ด 2 (3์ด์๋ ). surname state ๋ณ์๋ฅผ ์ฝ๋ ๋ฐ ์คํจํ์ต๋๋ค.
118
+ useEffect (updateTitle) // ๐ด 3 (4์๋ ). ์ ๋ชฉ์ ์
๋ฐ์ดํธํ๊ธฐ ์ํ effect๊ฐ ๋์ฒด๋๋ ๋ฐ ์คํจํ์ต๋๋ค.
119
119
```
120
120
121
- React wouldn't know what to return for the second ` useState ` Hook call. React expected that the second Hook call in this component corresponds to the ` persistForm ` effect, just like during the previous render, but it doesn't anymore. From that point, every next Hook call after the one we skipped would also shift by one, leading to bugs.
121
+ React๋ ๋ ๋ฒ์งธ ` useState ` Hook ํธ์ถ์ ๋ํด ๋ฌด์์ ๋ฐํํ ์ง ๋ชฐ๋์ต๋๋ค. React๋ ์ด์ ๋ ๋๋ง ๋์ฒ๋ผ ์ปดํฌ๋ํธ ๋ด์์ ๋ ๋ฒ์งธ Hook ํธ์ถ์ด ` persistForm ` effect์ ์ผ์นํ ๊ฒ์ด๋ผ ์์ํ์ง๋ง ๊ทธ๋ ์ง ์์์ต๋๋ค. ๊ทธ ์์ ๋ถํฐ ๊ฑด๋๋ด Hook ๋ค์์ ํธ์ถ๋๋ Hook์ด ์์๊ฐ ํ๋์ฉ ๋ฐ๋ฆฌ๋ฉด์ ๋ฒ๊ทธ๋ฅผ ๋ฐ์์ํค๊ฒ ๋ฉ๋๋ค.
122
122
123
- ** This is why Hooks must be called on the top level of our components .** If we want to run an effect conditionally, we can put that condition * inside * our Hook:
123
+ ** ์ด๊ฒ์ด ์ปดํฌ๋ํธ ์ต์์( the top of level)์์ Hook์ด ํธ์ถ๋์ด์ผ๋ง ํ๋ ์ด์ ์
๋๋ค .** ๋ง์ฝ์ ์กฐ๊ฑด๋ถ๋ก effect๋ฅผ ์คํํ๊ธฐ๋ฅผ ์ํ๋ค๋ฉด, ์กฐ๊ฑด๋ฌธ์ Hook * ๋ด๋ถ์ * ๋ฃ์ ์ ์์ต๋๋ค.
124
124
125
125
``` js
126
126
useEffect (function persistForm () {
127
- // ๐ We're not breaking the first rule anymore
127
+ // ๐ ๋ ์ด์ ์ฒซ ๋ฒ์งธ ๊ท์น์ ์ด๊ธฐ์ง ์์ต๋๋ค
128
128
if (name !== ' ' ) {
129
129
localStorage .setItem (' formData' , name);
130
130
}
131
131
});
132
132
```
133
133
134
- ** Note that you don't need to worry about this problem if you use the [ provided lint rule ] ( https://www.npmjs.com/package/eslint-plugin-react-hooks ) . ** But now you also know * why * Hooks work this way, and which issues the rule is preventing .
134
+ ** [ ์ ๊ณต๋ lint ๊ท์น ] ( https://www.npmjs.com/package/eslint-plugin-react-hooks ) ์ ํ์ฉํ๋ค๋ฉด ์ด ๋ฌธ์ ์ ๋ํด ๊ฑฑ์ ํ ํ์๋ ์์ต๋๋ค. ** ๊ทธ๋ฌ๋ ์ด์ ๋น์ ์ ์ Hook์ด ์ด๋ฐ ์์ผ๋ก ๋์ํ๋์ง ๊ทธ๋ฆฌ๊ณ ์ด ๊ท์น์ด ์ด๋ค ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๋์ง ์๊ณ ์์ต๋๋ค .
135
135
136
- ## Next Steps {#next-steps}
136
+ ## ๋ค์ ๋จ๊ณ {#next-steps}
137
137
138
- Finally, we're ready to learn about [ writing your own Hooks ] ( /docs/hooks-custom.html ) ! Custom Hooks let you combine Hooks provided by React into your own abstractions, and reuse common stateful logic between different components.
138
+ ๋ง์นจ๋ด [ Custom Hook ] ( /docs/hooks-custom.html ) ์ ์์ฑํ๋ ๋ฒ์ ๋ฐฐ์ธ ์ค๋น๊ฐ ๋์์ต๋๋ค ! Custom Hook์ React์์ ์ ๊ณตํ๋ Hook์ ๋น์ ์ ์ถ์ํ๋ ๋ก์ง์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ๊ฒฐํฉํด์ฃผ๊ณ ๋ค๋ฅธ ์ปดํฌ๋ํธ ์ฌ์ด์์ ๊ณตํต์ ์ํ ๊ด๋ จ ๋ก์ง์ ์ฌ์ฌ์ฉ ํ ์ ์๋๋ก ํด์ค๋๋ค.
0 commit comments