This might not be the optimal solution, but I would check if the return value from the validate function equals 0 in length, I know you are already doing this, but you are doing it after you send the API request. I would also recommend the use of promise chaining (developer.mozilla.org/en-US/docs/W...) for handling success and error cases on async functions such as API requests. As for displaying success or error messages, you could set some state variable just like you did with the error messages or what I have commonly used is toastr (github.com/CodeSeven/toastr) for easy popup notifications.
import{useState,useEffect}from"react";importaxiosfrom'axios';constuseForm=(callback,validate)=>{const[values,setValues]=useState({email:'',name:'',subject:'',description:''})const[errors,setErrors]=useState({});const[isSubmitting,setIsSubmitting]=useState(false);consthandleChange=event=>{const{name,value}=event.target;setValues({...values,[name]:value});};consthandleSubmit=(event)=>{event.preventDefault();setErrors(validate(values)).then((errors)=>{if(errors.length===0){const{email,name,subject,description}=values;axios.post('http://localhost:8080/sendme',{email,name,subject,text:description}).then((res)=>{console.log('Display your success here!');setIsSubmitting(false);}).catch((e)=>{console.log('Display your error here!');setIsSubmitting(false);});};});};useEffect(()=>{if(Object.keys(errors).length===0&&isSubmitting){callback();}},[callback,isSubmitting,errors]);return{handleChange,handleSubmit,values,errors};};exportdefaultuseForm;
Another solution would be to move the axios.get call to the useState call, after you have validated that errors is equal to 0, not sure what the optimal solution is here
(I haven't checked if this code will run, let me know if it does not work)
Thank you, since I posted this issue I switched to "react-hook-form" which worked like a charm, but I can see was wrong here, and used toastr to display my success message as you said and that saved me a lot of time.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This might not be the optimal solution, but I would check if the return value from the validate function equals 0 in length, I know you are already doing this, but you are doing it after you send the API request. I would also recommend the use of promise chaining (developer.mozilla.org/en-US/docs/W...) for handling success and error cases on async functions such as API requests. As for displaying success or error messages, you could set some state variable just like you did with the error messages or what I have commonly used is toastr (github.com/CodeSeven/toastr) for easy popup notifications.
Another solution would be to move the
axios.get
call to theuseState
call, after you have validated that errors is equal to 0, not sure what the optimal solution is here(I haven't checked if this code will run, let me know if it does not work)
Thank you, since I posted this issue I switched to "react-hook-form" which worked like a charm, but I can see was wrong here, and used toastr to display my success message as you said and that saved me a lot of time.