Skip to content

Commit 5e1ddf2

Browse files
Improve watch to reflect overloads (#1056)
* (watch): make first half reflect overloads * (watch): make examples heading real heading in html * Update watch.mdx --------- Co-authored-by: Beier (Bill) <[email protected]>
1 parent 28b970f commit 5e1ddf2

File tree

1 file changed

+88
-21
lines changed

1 file changed

+88
-21
lines changed

src/content/docs/useform/watch.mdx

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,113 @@ description: Subscribe to input changes
44
sidebar: apiLinks
55
---
66

7-
## \</> `watch:` <TypeText>(names?: string | string[] | (data, options) => void) => unknown</TypeText>
7+
## \</> `watch:` _<TypeText>some overloads</TypeText>_
88

99
This method will watch specified inputs and return their values. It is useful to render input value and for determining what to render by condition.
1010

11-
### Props
11+
### Overloads
12+
13+
This function mainly serves **two purposes**:
14+
15+
1. Returns and keep sync with fields' values
16+
a. <TypeText>`watch(name: string, defaultValue?): unknown`</TypeText>
17+
b. <TypeText>`watch(names: string[], defaultValue?): {[key:string]: unknown}`</TypeText>
18+
c. <TypeText>`watch(): {[key:string]: unknown}`</TypeText>
19+
2. Start subscribing with given callback function (can be stopped by calling `unsubscribe` function)
20+
a. <TypeText>`watch(callback: (data, { name, type }) => void, defaultValues?): { unsubscribe: () => void }`</TypeText>
21+
22+
The explanation of each of these four overloads follows below.
23+
24+
#### 1-a. Watching single field <TypeText>`watch(name: string, defaultValue?: unknown): unknown`</TypeText>
25+
26+
---
27+
28+
Watch and subscribe to a single field used outside of render.
29+
30+
**Params**
31+
32+
| Name | Type | Description |
33+
| -------------- | ------------------------------ | ----------------------------------- |
34+
| `name` | <TypeText>`string`</TypeText> | the field name |
35+
| `defaultValue` | <TypeText>`unknown`</TypeText> | _optional_. default value for field |
36+
37+
**Returns** the single field value.
38+
39+
```tsx
40+
const name = watch("name")
41+
```
42+
43+
#### 1-b. Watching some fields <TypeText>`watch(names: string[], defaultValue?: {[key:string]: unknown}): unknown[]`</TypeText>
1244

1345
---
1446

15-
| Type | Description |
16-
| ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- |
17-
| <TypeText>string</TypeText> | Watch input value by name (similar to lodash [get](https://lodash.com/docs/4.17.15#get) function) |
18-
| <TypeText>string[]</TypeText> | Watch multiple inputs |
19-
| <TypeText>undefined</TypeText> | Watch all inputs |
20-
| <TypeText>`(data: unknown, { name: string, type: string }) => void`</TypeText> | Watch all inputs and invoke a callback |
47+
Watch and subscribe to an array of fields used outside of render.
48+
49+
**Params**
50+
51+
| Name | Type | Description |
52+
| -------------- | ---------------------------------------------- | ------------------------------------- |
53+
| `names` | <TypeText>`string[]`</TypeText> | the field names |
54+
| `defaultValue` | <TypeText>`{[key:string]: unknown}`</TypeText> | _optional_. default values for fields |
55+
56+
**Returns** an array of field values.
57+
58+
```tsx
59+
const [name, name1] = watch(["name", "name1"])
60+
```
2161

22-
### Return
62+
#### 1-c. Watching the entire form <TypeText>`watch(): {[key:string]: unknown}`</TypeText>
2363

2464
---
2565

26-
| Example | Return |
27-
| ---------------------------------------------------------------- | -------------------------------------------------- |
28-
| `watch('inputName')` | <TypeText>unknown</TypeText> |
29-
| `watch(['inputName1'])` | <TypeText>unknown[]</TypeText> |
30-
| `watch()` | <TypeText>`{[key:string]: unknown}`</TypeText> |
31-
| `watch((data, { name, type }) => console.log(data, name, type))` | <TypeText>`{ unsubscribe: () => void }`</TypeText> |
66+
Watch and subscribe to the entire form update/change based on onChange and re-render at the useForm.
3267

33-
<Admonition type="important" title="Rules">
68+
**Params** None
69+
70+
**Returns** the entire form values.
71+
72+
```tsx
73+
const formValues = watch()
74+
```
75+
76+
#### 2. Start watching with callback fn <TypeText>`watch(callback: (data, { name, type }) => void, defaultValues?: {[key:string]: unknown}): { unsubscribe: () => void }`</TypeText>
77+
78+
---
79+
80+
Subscribe to field update/change without trigger re-render.
81+
82+
**Params**
83+
84+
| Name | Type | Description |
85+
| --------------- | ----------------------------------------------------- | ---------------------------------------------------- |
86+
| `callback` | <TypeText>`(data, { name, type }) => void`</TypeText> | callback function to subscribe to all fields changes |
87+
| `defaultValues` | <TypeText>`{[key:string]: unknown}`</TypeText> | _optional_. defaultValues for the entire form |
88+
89+
**Returns** object with `unsubscribe` function.
90+
91+
```tsx
92+
useEffect(() => {
93+
const { unsubscribe } = watch((value) => {
94+
console.log(value)
95+
})
96+
return () => unsubscribe()
97+
}, [watch])
98+
```
99+
100+
### Rules
101+
102+
---
34103

35104
- When `defaultValue` is not defined, the first render of `watch` will return `undefined` because it is called before `register`. It's **recommended** to provide `defaultValues` at `useForm` to avoid this behaviour, but you can set the inline `defaultValue` as the second argument.
36105
- When both `defaultValue` and `defaultValues` are supplied, `defaultValue` will be returned.
37106
- This API will trigger re-render at the root of your app or form, consider using a callback or the [useWatch](/docs/usewatch) api if you are experiencing performance issues.
38107
- `watch` result is optimised for render phase instead of `useEffect`'s deps, to detect value update you may want to use an external custom hook for value comparison.
39108

40-
</Admonition>
41-
42-
**Examples:**
109+
### Examples:
43110

44111
---
45112

46-
**Watch in a Form**
113+
#### Watch in a Form
47114

48115
<TabGroup buttonLabels={["TS", "JS"]}>
49116

@@ -138,7 +205,7 @@ function App() {
138205

139206
</TabGroup>
140207

141-
**Watch in Field Array**
208+
#### Watch in Field Array
142209

143210
<TabGroup buttonLabels={["TS", "JS"]}>
144211

0 commit comments

Comments
 (0)