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

110
Views
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
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!