how to use triggerValidation with useEffect
See original GitHub issueI want to run validation ckecks and display errors when the screen is initially displayed. ■what I use: react, react-hook-form, typescript ■detail: there are multiple inputs to check. Currently, no error is displayed unless 『setTimeout』is used. isLoaded is true if data is fetched. I think the timing of error display and readering is a problem. I don’t want to use setTimeout. Do you know any ideas, please help.(enough information?)
function RootComponent (props) {
useEffect(() => {
setTimeout(() => {
triggerValidation();
}, 1000);
}, [props.payload.isLoaded);
const REQUIRED_VALIDATE = { required: { value: true, message: REQUIRED_MESSAGE } };
const REGEX_NAME_NOT_EMPTY_VALIDATE = {
pattern: { value: /\S+/, message: REQUIRED_MESSAGE },
};
return (
<React.Fragment>
<TextField error={!!errors.familyName} name="familyName"
inputRef={register(
required: REQUIRED_VALIDATE.required,
pattern: REGEX_NAME_NOT_EMPTY_VALIDATE.pattern,
)} />
・・・and more
</React.Fragment>
);
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (9 by maintainers)
Top Results From Across the Web
How to validate using useEffect() - Stack Overflow
I am trying to validate form using useState() and useEffect(). Here's my useEffect() method: / for every change in our state this will...
Read more >useForm - trigger - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >How To Create and Validate a React Form With Hooks - Telerik
Today we'll cover how to create and validate a login form using React Hooks.
Read more >Form Validation Using Custom React Hooks - Upmostly
Form Validation Using Custom React Hooks ; Start by opening up the original project in your text editor, open Form.js ; When the...
Read more >React Hook Form: A guide with examples - LogRocket Blog
The register method helps you register an input field into React Hook Form so that it is available for the validation, and its...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@kotarella1110 @bluebill1049 could be run without using setTimeout. Thank you very much.
@shoko-eguchi You should execute
triggerValidation
when the store changes. Therefore, you need to provide the props received from the Store to the deps of useEffect.