Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
¿Por qué mi primera publicación no puede solicitar el éxito en los ganchos personalizados nativos de reacción con axios?

mis ganchos personalizados:

 import { useContext, useState } from "react"; import { AppContext } from "../context/app.context"; const usePostAxios = (url) => { const [isLoading, setIsLoading] = useState(false); const [data, setData] = useState(null); const [error, setError] = useState(null); const { setUrl, token } = useContext(AppContext); const postData = async (data) => { try { setIsLoading(true); axios.defaults.headers.common["X-Authreq"] = token; await axios .post(`${setUrl}/${url}`, data) .then(({ data }) => { setData(data); }) .catch((err) => { setError(err.response || err.request); }); setIsLoading(false); } catch (err) { console.log(err); } }; return [isLoading, postData, data, error]; }; export { usePostAxios };

para publicar datos:

 const [isPosting, postFun, res, err] = usePostAxios("/apidet/EmpLog"); const handleSubmit = async (val) => { await postFun({ UserName: val.email, password: val.pass, }); if (res) { const { Apd, Apt } = res.ResponseData; dispatch({ type: "APP-LOGIN", token: `${Apd}:${Apt}` }); setIsError(false); navigate("Home"); return; } else { setIsError(true); return; } };

entonces, ¿por qué mi código ingresa en el bloque else y obtiene un error, pero cuando presiono el botón Enviar nuevamente, el código se ejecuta correctamente y mi proceso de inicio de sesión es exitoso?

Entonces, ¿por qué mi código no se ejecuta en el primer intento?

¿Estoy haciendo algo estúpido?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Bueno, escrito de esta manera, básicamente, cuando se ejecuta este código

 if(res) { // Something to do with result }

la variable res tiene valor null , inicialmente.

En su lugar, debe verificar los cambios en res con useEffect , como:

 useEffect(()=> { if(res) { // Something to do with result } }, [res])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!