Skip to content

Commit d826ec0

Browse files
committed
revert tips section
1 parent 0f44ad4 commit d826ec0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/content/docs/useform/register.mdx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,52 @@ export default function App() {
245245
---
246246

247247
<YouTube youTubeId="JFIpCoajYkA" />
248+
249+
### Tips
250+
251+
---
252+
253+
#### Destructuring assignment
254+
255+
```javascript
256+
const { onChange, onBlur, name, ref } = register('firstName');
257+
// include type check against field path with the name you have supplied.
258+
259+
<input
260+
onChange={onChange} // assign onChange event
261+
onBlur={onBlur} // assign onBlur event
262+
name={name} // assign name prop
263+
ref={ref} // assign ref prop
264+
/>
265+
// same as above
266+
<input {...register('firstName')} />
267+
```
268+
269+
#### Custom Register
270+
271+
You can also register inputs with `useEffect` and treat them as virtual inputs. For controlled components, we provide a custom hook [useController](/docs/usecontroller) and [Controller](/docs/usecontroller/controller) component to take care this process for you.
272+
273+
If you choose to manually register fields, you will need to update the input value with [setValue](/docs/useform/setvalue).
274+
275+
```javascript
276+
register('firstName', { required: true, min: 8 });
277+
278+
<TextInput onTextChange={(value) => setValue('lastChange', value))} />
279+
```
280+
281+
#### How to work with `innerRef`, `inputRef`?
282+
283+
When the custom input component didn't expose ref correctly, you can get it working via the following.
284+
285+
```javascript
286+
// not working, because ref is not assigned
287+
<TextInput {...register('test')} />
288+
289+
const firstName = register('firstName', { required: true })
290+
<TextInput
291+
name={firstName.name}
292+
onChange={firstName.onChange}
293+
onBlur={firstName.onBlur}
294+
inputRef={firstName.ref} // you can achieve the same for different ref name such as innerRef
295+
/>
296+
```

0 commit comments

Comments
 (0)