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

292
Vistas
how to use redux useSelector to check state and do a fetch to a database

I am using redux in a project and I want to make a useSelector that would check to see if the values in the redux state are the default values if not it will do a request to the database and update the the state I feel like it is quite complicated though and I am having a hard time getting my head around how I need to do this. I need to do this because sometimes the correct state is not loaded in the state I am considering just doing a check every time I use useSelector to check if the values are the default values then fetch from the database but I would much prefer to write it a way that would allow to be handled within the redux selector but I can't really grasp I how I need to do it.

 const info =  useSelector(getInfo)

Ideally I would like the info to be handled when I fetch here

import { SET_USER_DETAILS } from "../constants/userConstants";

const intialState = {
  user: { },
};

const userReducer = (state = intialState, action: any) => {
  switch (action.type) {
    case SET_USER_DETAILS:
      return { ...state, user: action.payload };
    default:
      return state;
  }
};

here is what my current reducer looks like what would be the best way to do this as I am finding it a little bit difficult to follow the documentation on the redux website.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use redux-thunk. https://redux.js.org/usage/writing-logic-thunks then your thunk could look something like that:

const thunkFunction = (dispatch, getState) => {
  // logic here that can dispatch actions or read state

  const currentState = getState() as typeof initialState;
  // check if state is default state
  if (JSON.stringify(initialState) === JSON.stringify(currentState)) {
    fetch(url).then(data => {
      dispatch({type: SET_USER_DETAILS, payload: data})
    })
  }
}

You need first to fetch data in react component:

const MyComponent = () => {
  // while fetch is fetching, data will be default state,
  // and when fetch is done, that component will automatically
  // rerender with new data
  
  const data = useSelector(getInfo);
  const dispatch = useDispatch();
  
  useEffect(() => {
    dispatch(thunkFunction)
  },[])
  return <code>{JSON.stringify(data, null, 2)}</code>
}

I did not test it so may require some changes but in general concept is like this

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