title: Best practice for validating input while onChange inside React custom hook?
body: <p>I built a custom hook to handle a form. One thing I'm having trouble with is calling the validation while the input value is changing.</p>
  
  <p>I have four code snippets included. The second and third are just for context to show how the complete custom hook but feel free to skip them as I'm just curious about how to implement similar functionality from snippet 1 in snippet 4.</p>
  
  <p>The reason I want to do this, in addition to calling it on submit, is that if the input value becomes ' ' I would like to display the error message and when a user started typing it would go away.</p>
  
  <p>This was pretty simple when I wasn't using hooks I would just call a validate function after setState like this:</p>
  
  <pre><code>const validate = (name) =&gt; {
    switch(name):
      case "username":
        if(!values.username) {
          errors.username = "What's your username?";
        }
        break;
      default:
        if(!values.username) {
          errors.username = "What's your username?";
        }
  
        if(!values.password) {
          errors.username = "What's your password?";
        }
  
        break;
  }
  
  const handleChange = (e) =&gt; {
    let { name, value } = e.target; 
  
    this.setState({ ...values, 
      [name]: value 
    }, () =&gt; this.validate(name)) 
  }
  </code></pre>
  
  <p>So now using react hooks things are not as easy. I created a custom form handler that returns values, errors, handleChange, and handleSubmit. The form handler is passed an initialState, validate function, and a callback. As of now it looks like this:</p>
  
  <pre><code>import useForm from './form.handler.js';
  import validate from './form.validation.js';
  
  const schema = { username: "", password: "" }
  
  export default function Form() {
    const { values, errors, handleChange, handleSubmit } = useForm(schema, validate, submit);
  
    function submit() {
      console.log('submit:', values);
    }
  
    return (
      &lt;form&gt;&lt;/form&gt; // form stuff
    )
  }
  </code></pre>
  
  <p>Here's the validation file. It's simple, it just requires values for two fields.</p>
  
  <pre><code>export default function validate(values) {
    let errors = {};
  
    if(!values.username) {
      errors.username = "What's your username?";
    }
  
    if(!values.password) {
      errors.password = "What's your password?";
    }
  
    return errors;
  }
  </code></pre>
  
  <p>Now here is my form handler, where I'm trying to solve this problem. I have been trying different things around calling setErrors(validate(values)) in the useEffect but can't access the input. I'm not sure, but currently, the custom hook looks like this:</p>
  
  <pre><code>import { useState, useEffect, useCallback } from 'react';
  
  export default function useForm(schema, validate, callback) {
    const [values, setValues] = useState(schema),
          [errors, setErrors] = useState({}),
          [loading, setLoading] = useState(false); // true when form is submitting
  
    useEffect(() =&gt; {
      if(Object.keys(errors).length === 0 &amp;&amp; loading) {
          callback();
      }
  
      setLoading(false);
    }, [errors, loading, callback])
  
    // I see useCallback used for event handler's. Not part of my questions, but is it to prevent possible memory leak?
    const handleChange = (e) =&gt; {
      let { name, value } = e.target;
  
      setValues({ ...values, [name]: value });
    }
  
    const handleSubmit = (e) =&gt; {
      e.preventDefault();
  
      setLoading(true);
      setErrors(validate(values));
    }
  
    return { values, errors, handleChange, handleSubmit }
  }
  </code></pre>