1

I have a listing page of Products that I intend to server render. So using NextJs's getInitialProps I fetch the list of Products and pass it on to the components and it works.

There is a requirement, whenever a city changes(from Dropdown) in client-side, I need to refetch the updated list of Products from the API. Hence, I have this API call in a useEffect. This is somewhat the idea.

ProductsWrapper.getInitialProps = async () => {
 // This runs on Server Side
 // fetch list of Products
 const products = await getProducts();
 // update the redux store with products returned from server by dispatching
 return { products };
}

// Products is a redux connected component that listens for Product and City state updates
function Products({ products, city }) {

 // fetch new Products iff city changes
 useEffect( async () => {
   // This runs only on Browser
   const products = await getProducts();
    // update the redux store with products returned from server
 }, [city])
}

The problem is at step 3 below,

  1. At the server Products API call is made and fed to Redux.
  2. The Page is built at the server-side and sent over to the client.
  3. At the Client, useEffect runs and unnecessarily calls the API endpoint again.

How do I Prevent this redundant call, the first time when the page is rendered on the client?

Well, just an if check inside useEffect whether data is present before making the api call doesn't work, because despite Products data being present in Redux store, if the city changes, the api call has to be made.

5
  • how are you able to use await without async? Commented Jul 13, 2020 at 13:53
  • @Ifaruki Sorry missed that was a typo Commented Jul 13, 2020 at 13:55
  • Can you call getProducts in the city change handler? Commented Jul 14, 2020 at 3:44
  • @AryanJ-NYC Even if we do that, problem would still happen bcoz the root issue is 1. If there is a same network request in getInitialProps and useEffect, A second redundant network request is bound to happen coz of useEffect running on client end. Do u agree? Commented Jul 14, 2020 at 4:32
  • 1
    I think useEffect is not the correct way to handle it. You need to fetch data when that event happens. (City change) That'll fix the issue. Commented Jul 14, 2020 at 5:07

1 Answer 1

1

The redundant call is due to the useEffect usage.

A better alternative by the name of getServerSideProps is available for this use case. https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering

This makes sure that the given fetch call is only run on server.

After this if you want to refetch on the change of dropdown you can either call it again at onChange or route to a different URL(e.g. /city/foobar).

In both cases there shouldn't be any redundant call on client as we did not use the useEffect hook

Sign up to request clarification or add additional context in comments.

Comments

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.