Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
Problem with reset state function and only than fetching data (React hooks)

I am showing a list of users(profiles), and fetch it from some users DB. I am in the search page which include sub pages for diffrenet filters - like which users are currently online. Each time i am moving inside the search sub pages, i have to reset only once the main filtering variable in order ot get the correct result. The problem is the fetch request happpend before the setState variable changed. I saw other people asked how to fetch after, while i need it to first reset the variables of setState and the to go and fetch according to the correct values.

code:

const [isPopUpShowState,setIsPopUpShowState] = useState(false);        
const [profilesloading,setProfilesLoading] = useState(<Spinner/>);          
const [profilesLength,setProfilesLength] = useState(0);    
const [profilesPerPage] = useState(4);                      
const [searchStartPoint,setSearchStartPoint] = useState(0); 
const [lastUserConnIndex,setLastUserConnIndex] = useState(1);   

       
useEffect( ()=> {
    restoreStatesToDefault();  // reset states+list  --> the variables doesnt changed before the the fetch       
       
   getProfilesMatchingPage(); // init profiles
},[history.location.pathname]);

const restoreStatesToDefault = () => {               
    list = {};
    setSearchStartPoint(0);
    setLastUserConnIndex(1);                            
    setProfilesLength(0);        
}

const getSearchProfilesParmsInObj = () => {
   const parmsObj = {};
   if(currUser.loginObj){            
     parmsObj['isMale'] = !currUser.loginObj.data.isMale;
     parmsObj['profilesPerPage'] = profilesPerPage;           
     parmsObj['searchStartPoint'] = searchStartPoint;
     parmsObj['lastUserConnIndex'] = lastUserConnIndex;            
     parmsObj['allProfiles'] = list;
    }
    return parmsObj;
}

    const getProfilesMatchingPage = () => {
           
            switch(history.location.pathname){
                case '/search/online':                              
                    dispatch(getProfilesOnline(getSearchProfilesParmsInObj(),setProfilesLoading,setLastUserConnIndex,setProfilesLength));
                    break;            
                case '/search/pics':
                    dispatch(getProfilesOnlyWithPics(getSearchProfilesParmsInObj(),setProfilesLoading,setLastUserConnIndex,setSearchStartPoint,setProfilesLength));  
                    break;
                case '/search/recently':
                    dispatch(getProfilesRecentlyVisited(getSearchProfilesParmsInObj(),setProfilesLoading,setLastUserConnIndex,setSearchStartPoint,setProfilesLength));  
                    break;
                case '/search/news':
                    dispatch(getProfilesNewUsersRegistered(getSearchProfilesParmsInObj(),setProfilesLoading,setLastUserConnIndex,setSearchStartPoint,setProfilesLength));        
            }
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is that both functions are called within the same lifecycle of the function, so the states haven't updated yet (They are within the same closure). After your useEffect finishes, then the next render is called with the updated state values, but they are not dependencies of your useEffect so they don't trigger it to fire again (which is a good thing in this case).

Basically what you want is two useEffect -> one is triggered on path change, and that one should update state that is a dependency of another useEffect that triggers the fetch.

A simple example would be:

const [shouldFetch, setShouldFetch] = useState(false) // Set this to true if you want to fetch on initial render
useEffect( ()=> {
    restoreStatesToDefault();  // reset states+list  --> the variables doesnt changed before the the fetch       
    setShouldFetch(true);
},[history.location.pathname]);

useEffect(() => {
    if (shouldFetch) {
        setShouldFetch(false);
        getProfilesMatchingPage(); // init profiles
    }
}, [shouldFetch])

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda