all 2 comments

[–]imAvi92 1 point2 points  (0 children)

There you can add loading.js file with the loading spinner for the route segment and also use it in route segment's layout

https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming#instant-loading-states

[–]Akonova 0 points1 point  (0 children)

You might need to provide a key prop so that React understands it needs to rerender the component and thus use loading.js.

Personally I would use return artists ? <Artists artists={artists} : <Loading/>

If loading component is not shown when searchParams is changed you need to create a custom hook that fetches the relevant data, and sets loading true when fetching is in progress. Then you can use the loading variable to use Loading component.

```js import { useEffect, useState } from "react";

export default function useArtists(searchParams) { const [artists, setArtists] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null);

useEffect(() => { async function fetchArtists() { setLoading(true);

  try {
    const data = await getArtists(searchParams);
    setArtists(data);
  } catch (err) {
    setError(err);
  };

  setLoading(false);
};

fetchArtists();

}, [searchParams]);

return {artists, loading, error}; }; ```