-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathUseWatchContent.tsx
More file actions
233 lines (221 loc) Β· 6.9 KB
/
UseWatchContent.tsx
File metadata and controls
233 lines (221 loc) Β· 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import * as React from "react"
import CodeArea from "./CodeArea"
import useWatch from "./codeExamples/useWatch"
import useWatchTs from "./codeExamples/useWatchTs"
import generic from "../data/generic"
import * as typographyStyles from "../styles/typography.module.css"
import * as tableStyles from "../styles/table.module.css"
import TabGroup from "./TabGroup"
import useWatchFieldArray from "./codeExamples/useWatchFieldArray"
import { Link } from "gatsby"
export default function UseFieldArray({
api,
currentLanguage,
}: {
currentLanguage: string
api: any
}) {
return (
<>
<code className={typographyStyles.codeHeading}>
<h2>
useWatch: <br />
<span
className={typographyStyles.typeText}
>{`({ control?: Control, name?: string, defaultValue?: unknown, disabled?: boolean }) => object`}</span>
</h2>
</code>
{api.useWatch.description}
<h2 className={typographyStyles.subTitle}>Props</h2>
<div className={tableStyles.tableWrapper}>
<table className={tableStyles.table}>
<thead>
<tr>
<th>{generic.name[currentLanguage]}</th>
<th>{generic.type[currentLanguage]}</th>
{/*<th width="90px">{generic.required[currentLanguage]}</th>*/}
<th>{generic.description[currentLanguage]}</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>name</code>
</td>
<td>
<code className={typographyStyles.typeText}>
string | string[] | undefined
</code>
</td>
<td>{api.ErrorMessage.table.name}</td>
</tr>
<tr>
<td>
<code>control</code>
</td>
<td>
<code className={typographyStyles.typeText}>Object</code>
</td>
<td>{generic.control[currentLanguage]}</td>
</tr>
<tr>
<td>
<code>defaultValue</code>
</td>
<td>
<code className={typographyStyles.typeText}>unknown</code>
</td>
<td>
<p>
default value for <code>useWatch</code> to return before the
initial render.
</p>
<p>
<b className={typographyStyles.note}>Note:</b> the first
render will always return <code>defaultValue</code> when it's
supplied.
</p>
</td>
</tr>
<tr>
<td>
<code>disabled</code>
</td>
<td>
<code className={typographyStyles.typeText}>
boolean = false
</code>
</td>
<td>
<p>Option to disable the subscription.</p>
</td>
</tr>
<tr>
<td>
<code>exact</code>
</td>
<td>
<code className={typographyStyles.typeText}>
boolean = false
</code>
</td>
<td>
<p>
This prop will enable an exact match for input name
subscriptions.
</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2 className={typographyStyles.subTitle}>Return</h2>
<div className={tableStyles.tableWrapper}>
<table className={tableStyles.table}>
<tbody>
<tr>
<th
style={{
minWidth: 340,
}}
>
{generic.example[currentLanguage]}
</th>
<th
style={{
minWidth: 180,
}}
>
{generic.return[currentLanguage]}
</th>
</tr>
<tr>
<td>
<code>useWatch('inputName')</code>
</td>
<td>
<code className={typographyStyles.typeText}>unknown</code>
</td>
</tr>
<tr>
<td>
<code>useWatch(['inputName1'])</code>
</td>
<td>
<code className={typographyStyles.typeText}>unknown[]</code>
</td>
</tr>
<tr>
<td>
<code>useWatch()</code>
</td>
<td>
<code
className={typographyStyles.typeText}
>{`{[key:string]: unknown}`}</code>
</td>
</tr>
</tbody>
</table>
</div>
<h2 id="rules" className={typographyStyles.rulesTitle}>
Rules
</h2>
<ul>
<li>
<p>
The initial return value from <code>useWatch</code> will always
return what's inside of <code>defaultValue</code> or{" "}
<code>defaultValues</code> from <code>useForm</code>.
</p>
</li>
<li>
<p>
The only difference between <code>useWatch</code> and{" "}
<code>watch</code> is at the root (
<Link to="/api/useform">
<code>useForm</code>
</Link>
) level or the custom hook level update.
</p>
</li>
<li>
<p>
<code>useWatch</code>'s execution order matters, which means if you
update a form value before the subscription is in place, then the
value updated will be ignored.
</p>
<CodeArea
rawData={`setValue('test', 'data');
useWatch({ name: 'test' }); // β subscription is happened after value update, no update received
useWatch({ name: 'example' }); // β
input value update will be received and trigger re-render
setValue('example', 'data');
`}
/>
</li>
<li>
<p>
<code>useWatch</code>'s result is optimised for render phase instead
of <code>useEffect</code>'s deps, to detect value updates you may
want to use an external custom hook for value comparison.
</p>
</li>
</ul>
<h2 id="example" className={typographyStyles.subTitle}>
Examples
</h2>
<TabGroup buttonLabels={["Form", "Advance Field Array"]}>
<CodeArea
rawData={useWatch}
url="https://codesandbox.io/s/react-hook-form-v7-usewatch-forked-9872t"
tsRawData={useWatchTs}
tsUrl="https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e"
/>
<CodeArea
rawData={useWatchFieldArray}
url="https://codesandbox.io/s/watchusewatch-calc-4tpnh"
/>
</TabGroup>
</>
)
}