We are developing an universal web app using react-redux. I can see data fetching is so slick in hashnode. As soon as i navigate to route , view is up with necessary data. Whereas in our case we are dispatching actions to fetch data in componentDidMount event. It's not as smooth as i can see in hashnode. How can we achieve UX close to this one?
Sandeep Panda
co-founder, Hashnode
You are seeing this behaviour on Hashnode as our Flux Stores are populated before the route transition occurs. In your case, you are dispatching the actions after the component mounts and that's why for a small period of time there will be no data to show in the view. In our case before the component mounts the data is readily available.
Solution : Dispatch the action and load data before route transition occurs.
UPDATE :
More details as per requests from @evivz and @Omoayo :
If you are using Redux, check out this line in our mern-starter project. You can simply define the
needsof a component which means you are supplying a list of actions that need to be dispatched before the component renders. This should do what you are looking for.If you are not using Redux or just using simple Flux implementations, you could do something like this :
PostActions.loadPost(postId,function(){ // The data is available and stores are populated at this point. //It's now safe to make a route transition. this.context.router.transitionTo('someRoute'); }.bind(this));PostActionsshould make an xhr and populate the stores before route transition occurs.