-1

this is my code everything is ok but if statement doesn't work if i erase the if part of code the request works just fine but when it's inside of if it does't work my errors object is empty

 const handleSubmit=(e)=> {
    e.preventDefault();
    setErrors(validate(values))

      if (!errors) {
          console.log(errors);
          baseUrl.get("/sanctum/csrf-cookie").then((response) => {
            baseUrl
              .post("/api/v1/admin-login", values)
              .then((response) => {
                console.log(response);
              })
              .catch((err) => {});
          });
     }
        

and the errors state

  const [errors, setErrors] = useState({});
saeed
  • 69
  • 1
  • 9

2 Answers2

0

that's because the value of error is not changed yet.

you can fix it in this way


const [errors, setErrors] = useState({})

const handleSubmit=(e)=> {
    e.preventDefault();
    const errors = validate(values) 
    setErrors(errors)

      if (!errors) {
          console.log(errors);
          baseUrl.get("/sanctum/csrf-cookie").then((response) => {
            baseUrl
              .post("/api/v1/admin-login", values)
              .then((response) => {
                console.log(response);
              })
              .catch((err) => {});
          });
     }
R4ncid
  • 6,944
  • 1
  • 4
  • 18
0

The value has not changed yet because you set errors for the update state in the current function and the next line you want to use. You can use the local variable in the function to check errors.

setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this. state right after calling setState() a potential pitfall. https://reactjs.org/docs/react-component.html#setstate

const handleSubmit=(e)=> {
e.preventDefault();
const errors = validate(values) 
setErrors(errors)
  if (!errors) {
      console.log(errors);
      baseUrl.get("/sanctum/csrf-cookie").then((response) => {
        baseUrl
          .post("/api/v1/admin-login", values)
          .then((response) => {
            console.log(response);
          })
          .catch((err) => {});
      });
 }
Iman Rabbi
  • 271
  • 1
  • 2
  • still the same it doesn't work :( – saeed Jan 26 '22 at 07:40
  • what does `validate(values)` return? – Iman Rabbi Jan 26 '22 at 07:42
  • yes it returns just fine the request part doesn't work but without setting a condition it woks just fine – saeed Jan 26 '22 at 07:45
  • 1
    if your `validate(values)` returns empty object `{}` and if you try to check `!errors` it will always returns false. To check if object is empty then you can use `isEmpty` method from lodash or underscore library or you can create your own function to check if object is empty or not. Check this answer for that: https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – Priyank Kachhela Jan 26 '22 at 08:00
  • thank you iman i didn't know that it works know ;) – saeed Jan 26 '22 at 08:05