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

118
Visualizações
How to start from the initial state in React app with Redux store?

I have a React app with Redux store. I'm using two pages - index page and edit post page. When user goes to edit page it loads the post content from the server and setting it in Redux store. Code looks like this:

const EditPost = props => {
   const { postId } = useParams();
   const { isLoading, error, post } = useSelector(state => state.post);
   const dispatch = useDispatch();

   useEffect(
      () => {
         if (postId) dispatch(getPost(postId));
      },
      [dispatch, postId]
   );

   if (isLoading) {
      return <div>Post is loading...</div>
   }

   if (error) {
      return <div>Error: {error}</div>
   }

   return (
      <div>Post content here...</div>
   )
}

The problem is when user navigate to index page the post in Redux is already setted and user sees the last edited post instead default index post. The code of index page is:

const Index = props => {
   const { isLoading, error, post } = useSelector(state => state.post);
   const dispatch = useDispatch();
   
   const postId = defaultPostId;
   
   useEffect(
      () => {
         dispatch(getPost(postId));
      },
      [dispatch, postId]
   );

   if (isLoading) {
      return <div>Post is Loading...</div>
   }

   if (error) {
      return <div>Error: {error}</div>
   }

   return (
      <div>Default index post here...</div>
   )
}

My desired behavior has to be like if user goes to index page after edit post it should see the loading and then the default post content. But the current situation is user sees the last edited post, then loading process and then it shows default post. Obviously because Redux contains last post data in store.

So the question is: How and when to set post to default state? I assume it's related to lifecycles.

P.S getPost action code looks like this:

export const getPost = postId => {
   return async (dispatch, getState) => {
      try {
         dispatch(isPostLoading(true));
         const { data: post } = await req.get(`/post/${postId}`);
         dispatch(setPost(post));
         dispatch(setPostError(null));
      } catch (error) {
         setPostError(error.message);
      }
      dispatch(isPostLoading(false));
   }
}
about 4 years ago · Juan Pablo Isaza
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