question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to use triggerValidation with useEffect

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
shoko-eguchicommented, Mar 30, 2020

@kotarella1110 @bluebill1049 could be run without using setTimeout. Thank you very much.

2reactions
kotarella1110commented, Mar 27, 2020

@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.

useEffect(() => {
  triggerValidation();
}, [
  props.data.name,
  props.data.familyName,
  props.data.phoneNumber01,
  props.data.phoneNumber02
]);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found