1- <div align =" center " ><h1 >📣 UPDATE</h1 >
2-
3- This component is now a part of React Hook Form V4, and renamed to <a href =" https://react-hook-form.com/api/#ErrorMessage " >ErrorMessage</a >.
4-
5- </div >
6-
71<div align =" center " >
82 <p align="center">
93 <a href="https://react-hook-form.com" title="React Hook Form - Simple React forms validation">
@@ -16,45 +10,113 @@ This component is now a part of React Hook Form V4, and renamed to <a href="http
1610
1711<div align =" center " >
1812
19- [ ![ npm downloads] ( https://img.shields.io/npm/dm/react-hook-form-error.svg?style=flat-square )] ( https://www.npmjs.com/package/react-hook-form-error )
20- [ ![ npm] ( https://img.shields.io/npm/dt/react-hook-form-error.svg?style=flat-square )] ( https://www.npmjs.com/package/react-hook-form-error )
21- [ ![ npm] ( https://badgen.net/bundlephobia/minzip/react-hook-form-error )] ( https://badgen.net/bundlephobia/minzip/react-hook-form-error )
22- [ ![ Tweet] ( https://img.shields.io/twitter/url/http/shields.io.svg?style=social )] ( https://twitter.com/intent/tweet?text=React+hooks+for+form+validation+without+the+hassle&url=https://github.com/bluebill1049/react-hook-form-error )   ; [ ![ Join the community on Spectrum] ( https://withspectrum.github.io/badge/badge.svg )] ( https://spectrum.chat/react-hook-form-error )
13+ [ ![ npm downloads] ( https://img.shields.io/npm/dm/error-message.svg?style=for-the-badge )] ( https://www.npmjs.com/package/@hookform/error-message )
14+ [ ![ npm] ( https://img.shields.io/npm/dt/@hookform/error-message.svg?style=for-the-badge )] ( https://www.npmjs.com/package/@hookform/error-message )
15+ [ ![ npm] ( https://badgen.net/bundlephobia/minzip/@hookform/error-message?style=for-the-badge )] ( https://bundlephobia.com/result?p=@hookform/error-message )
2316
2417</div >
2518
19+ ## Goal
20+
21+ A simple component to render associated input's error message.
22+
2623## Install
2724
28- $ npm install react-hook-form-error
25+ ```
26+ $ npm install @hookform/error-message
27+ ```
2928
3029## Quickstart
3130
31+ ### Single Error Message
32+
33+ ``` jsx
34+ import React from ' react' ;
35+ import { useForm } from ' react-hook-form' ;
36+ import { ErrorMessage } from ' hookform@error-message' ;
37+
38+ export default function App () {
39+ const { register , errors , handleSubmit } = useForm ();
40+ const onSubmit = (data ) => console .log (data);
41+
42+ return (
43+ < form onSubmit= {handleSubmit (onSubmit)}>
44+ < input
45+ name= " singleErrorInput"
46+ ref= {register ({ required: ' This is required.' })}
47+ / >
48+ < ErrorMessage errors= {errors} name= " singleErrorInput" / >
49+
50+ < ErrorMessage
51+ errors= {errors}
52+ name= " singleErrorInput"
53+ render= {({ message }) => < p> {message}< / p> }
54+ / >
55+
56+ < input name= " name" ref= {register ({ required: true })} / >
57+ < ErrorMessage errors= {errors} name= " name" message= " This is required" / >
58+
59+ < input type= " submit" / >
60+ < / form>
61+ );
62+ }
63+ ```
64+
65+ ### Multiple Error Messages
66+
3267``` jsx
3368import React from ' react' ;
34- import useForm from ' react-hook-form' ;
35- import { RHFError } from ' react-hook-form- error' ;
69+ import { useForm } from ' react-hook-form' ;
70+ import { ErrorMessage } from ' @hookform/ error-messagee ' ;
3671
37- function App () {
38- const { handleSubmit , register , errors } = useForm ();
72+ export default function App () {
73+ const { register , errors , handleSubmit } = useForm ({
74+ validateCriteriaMode: ' all' ,
75+ });
76+ const onSubmit = (data ) => console .log (data);
3977
4078 return (
41- < form onSubmit= {handleSubmit (data => console .log (data))}>
42- < input name= " test" ref= {register} / >
43- < RHFError name= " test" errors= {errors} / >
44- < button> submit< / button>
79+ < form onSubmit= {handleSubmit (onSubmit)}>
80+ < input
81+ name= " multipleErrorInput"
82+ ref= {register ({
83+ required: ' This is required.' ,
84+ pattern: {
85+ value: / d+ / ,
86+ message: ' This input is number only.' ,
87+ },
88+ maxLength: {
89+ value: 10 ,
90+ message: ' This input exceed maxLength.' ,
91+ },
92+ })}
93+ / >
94+ < ErrorMessage
95+ errors= {errors}
96+ name= " multipleErrorInput"
97+ render= {({ messages }) =>
98+ messages &&
99+ Object .entries (messages).map (([type , message ]) => (
100+ < p key= {type}> {message}< / p>
101+ ))
102+ }
103+ / >
104+
105+ < input type= " submit" / >
45106 < / form>
46107 );
47108}
48109```
49110
50111## API
51112
52- | Prop | Type | Required | Default | Description |
53- | :--------- | :-------- | :------: | :-----: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54- | ` name ` | string | ✓ | | Unique name to register the custom input |
55- | ` 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 > |
56- | ` as ` | Component | | | Component reference eg: ` <span /> ` |
57- | ` messages ` | Object | | | keys of error type's message |
113+ | Prop | Type | Required | Default | Description |
114+ | :-------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------: | :-----: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
115+ | ` name ` | string | ✓ | | associated field name. |
116+ | ` errors ` | object | | | errors object from React Hook Form. It's optional if you are using ` FormProvider ` . |
117+ | ` message ` | string \| React.ReactElement | | | inline error message. |
118+ | ` as ` | string \| <br >React.ReactElement \| <br >React.ComponentType | | | Wrapper component or HTML tag. eg: ` as="p" ` , ` as={<p />} ` or ` as={CustomComponent} ` |
119+ | ` render ` | (payload: {<br >  ;  ; message: string \| React.ReactElement;<br >  ;  ; messages?: Record<<br >  ;  ;  ;  ; string,<br >  ;  ;  ;  ; (string \| React.ReactElement \| boolean \| undefined)[ ] <br >  ;  ; >;<br >}) => React.ReactNode | | | This is a [ render prop] ( https://reactjs.org/docs/render-props.html ) for rendering error message or messages. <br >Note: you need to set validateCriteriaMode to 'all' for using messages. |
58120
59121## Backers
60122
0 commit comments