-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathApiWatch.tsx
219 lines (205 loc) Β· 6.49 KB
/
ApiWatch.tsx
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
import * as React from "react"
import CodeArea from "./CodeArea"
import watchCode from "./codeExamples/watchCode"
import watchCodeTs from "./codeExamples/watchCodeTs"
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 watchFieldArrayCode from "./codeExamples/watchFieldArrayCode"
import StarRepo from "./StarRepo"
import { Link } from "gatsby"
export default function ApiWatch({
currentLanguage,
api,
}: {
api: any
currentLanguage: string
}) {
return (
<>
<code className={typographyStyles.codeHeading}>
<h2>
watch:{" "}
<span
className={typographyStyles.typeText}
>{`(names?: string | string[] | (data, options) => void) => unknown`}</span>
</h2>
</code>
{api.watch.description}
<h2 className={typographyStyles.subTitle}>Props</h2>
<div className={tableStyles.tableWrapper}>
<table className={tableStyles.table}>
<tbody>
<tr>
<th
style={{
minWidth: 180,
}}
>
{generic.type[currentLanguage]}
</th>
<th
style={{
minWidth: 200,
}}
>
{generic.description[currentLanguage]}
</th>
</tr>
<tr>
<td>
<code className={typographyStyles.typeText}>string</code>
</td>
<td>{api.watch.tableTitle.single}</td>
</tr>
<tr>
<td>
<code className={typographyStyles.typeText}>string[]</code>
</td>
<td>{api.watch.tableTitle.multiple}</td>
</tr>
<tr>
<td>
<code className={typographyStyles.typeText}>undefined</code>
</td>
<td>{api.watch.tableTitle.all}</td>
</tr>
<tr>
<td>
<code
className={typographyStyles.typeText}
>{`(data: unknown, { name: string, type: string }) => void`}</code>
</td>
<td>{api.watch.tableTitle.callback}</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>watch('inputName')</code>
</td>
<td>
<code className={typographyStyles.typeText}>unknown</code>
</td>
</tr>
<tr>
<td>
<code>watch(['inputName1'])</code>
</td>
<td>
<code className={typographyStyles.typeText}>unknown[]</code>
</td>
</tr>
<tr>
<td>
<code>watch()</code>
</td>
<td>
<code
className={typographyStyles.typeText}
>{`{[key:string]: unknown}`}</code>
</td>
</tr>
<tr>
<td>
<code>{`watch((data, { name, type }) => console.log(data, name, type))`}</code>
</td>
<td>
<code
className={typographyStyles.typeText}
>{`{ unsubscribe: () => void }`}</code>
</td>
</tr>
</tbody>
</table>
</div>
<h2 id="rules" className={typographyStyles.rulesTitle}>
Rules
</h2>
<ul>
<li>
<p>
When <code>defaultValue</code> is not defined, the first render of{" "}
<code>watch</code> will return <code>undefined</code> because it is
called before <code>register</code>. It's <b>recommend</b> to
provide <code>defaultValues</code> at <code>useForm</code> to avoid
this behaviour, but you can set the inline <code>defaultValue</code>{" "}
as the second argument.
</p>
</li>
<li>
<p>
When both <code>defaultValue</code> and <code>defaultValues</code>{" "}
are supplied, <code>defaultValue</code> will be returned.
</p>
</li>
<li>
<p>
This API will trigger re-render at the root of your app or form,
consider using a callback or the{" "}
<Link to="/api/usewatch">useWatch</Link> api if you are experiencing
performance issues.
</p>
</li>
<li>
<p>
<code>watch</code> result is optimised for render phase instead of{" "}
<code>useEffect</code>'s deps, to detect value update 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={watchCode}
tsRawData={watchCodeTs}
url="https://codesandbox.io/s/react-hook-form-watch-v7-qbxd7"
tsUrl="https://codesandbox.io/s/react-hook-form-watch-v7-ts-8et1d"
/>
<CodeArea
rawData={watchFieldArrayCode}
url="https://codesandbox.io/s/watch-with-usefieldarray-e2d64"
/>
</TabGroup>
<h2 className={typographyStyles.subTitle}>Video</h2>
<p>
The following video tutorial demonstrates <code>watch</code> API.
</p>
<iframe
width="100%"
height="528"
title="react hook form controller build"
src="https://www.youtube.com/embed/3qLd69WMqKk"
frameBorder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
/>
<StarRepo currentLanguage="en" />
</>
)
}