2

My application has to make an API call, that returns an array of items that gets styled and rendered to the screen. To make my SSR rendering work, I have to make the API request on server of course, but when using the SSR you also have to rerender (using ReactDOM.hydrate) on the client and I'm making the 2nd API request in here.

Since the API request takes more than 2 seconds, doing it 2 times is so inefficient. Is there a workaround around that by only doing 1 request and someway using the data on both server and client?

1 Answer 1

2

This is how you fetch data on a server and reuse it on client - straight from the redux documentation. If you are not using redux you will need to implement something very similar.

To answer the latter question - how to avoid making another request on the client? You just need to check if the data is there :)

componentDidMount() {
  if(!this.props.isDataFetched) {
    this.props.fetchData();
  }
}

or maybe...

componentDidMount() {
  if(this.props.data.length === 0) {
    this.props.fetchData();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, I am not using Redux for my application and I won't even have to. As I noticed, they just save the data to the window which I've encountered before and since the data doesn't need to have any security at all, it is a fine option for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.