Skip to content

Commit 9142024

Browse files
committed
update readme
1 parent 7dfa49b commit 9142024

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,31 @@ Check out this <a href="https://codesandbox.io/s/react-hook-form-hookforminput-r
2828
## Quickstart
2929

3030
```jsx
31+
import React from 'react';
32+
import useForm from 'react-hook-form';
33+
import { RHFError } from 'react-hook-form-error';
34+
35+
function App() {
36+
const { handleSubmit, register, errors } = useForm();
37+
38+
return (
39+
<form onSubmit={handleSubmit(data => console.log(data))}>
40+
<input name="test" ref={register} />
41+
<RHFError name="test" errors={errors} />
42+
<button>submit</button>
43+
</form>
44+
);
45+
}
3146
```
3247

3348
## API
34-
|
49+
50+
| Prop | Type | Required | Default | Description |
51+
| :--------- | :-------- | :------: | :-----: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52+
| `name` | string || | Unique name to register the custom input |
53+
| `errors` | Object || | (optional when using <a href="https://react-hook-form.com/api#errors">errors</a>) React Hook Form <a href="https://react-hook-form.com/api#setValue">errors</a> |
54+
| `as` | Component | | | Component reference eg: `Select` from `react-select` |
55+
| `messages` | Object | | | keys of error type's message |
3556

3657
## Contributors
3758

src/index.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as React from 'react';
2+
import { useFormContext } from 'react-hook-form';
23

34
type ErrorFields = Record<string, { message?: string; type: string }>;
45

56
const RHFError = <Errors extends ErrorFields, Name extends keyof Errors>({
67
as,
7-
errors,
8+
errors: errorsFromProps,
89
name,
910
messages = {},
1011
}: {
@@ -13,6 +14,8 @@ const RHFError = <Errors extends ErrorFields, Name extends keyof Errors>({
1314
name: Name;
1415
messages?: Record<string, string>;
1516
}) => {
17+
const methods = useFormContext();
18+
const errors = errorsFromProps || methods.errors;
1619
const message = errors[name].message || messages[errors[name].type];
1720

1821
if (!message) {

0 commit comments

Comments
 (0)