Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda