Next.js Discord

Discord Forum

Correct way of handling tab components, updating url path, and preserving fetched data on navigation

Answered
Tonkinese posted this in #help-forum
Open in Discord
TonkineseOP
Hey guys, is it expected behavior that anything inside a Tab component will "load" [1] any time i view the page containing that component? ([1] not sure what the correct term here is, but basically my fetch query re-runs each time i view the page, as opposed to other pages where the skeleton appears the first time you view the page and then the content stays when navigating backwards or forwards). Here's the relevant code:

--- users/[username]/page.tsx

const Page: FC<PageProps> = ({ params: { username } }) => {
  return <UserPage username={username} />;
};

--- UserPage.tsx ---

export const UserPage: FC<UserPageProps> = ({ username }) => {
  const result = useQuery(GET_USER_QUERY, {
    variables: {
      username: username,
    },
  });

  return <UserComponent result={result} />;
};

--- UserComponent.tsx ---

export const UserComponent = ({ result }: UserComponentProps) => {
  const router = useRouter();
  const session = useSession();
  const searchParams = useSearchParams();

  const { data, loading, error } = result;

  const user = data?.user;

  const tab: any = searchParams?.toString().replace("=", "");

  const handleTabClick = (tab: any) => {
    router.push(`/users/${user.username}?${tab}`);
  }
}


what's the recommended way of updating the url with tab selection data without having this reload issue happen?
Answered by B33fb0n3
Yea, that is possible thought parallel routes. You might want to take a look at them: https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
View full answer

18 Replies

TonkineseOP
from an adjacent discussion: "useSearchParams – this requires a Suspense or dynamic rendering, both of which i assume mess with bfcache"
for context, this is the sort of layout I'm aiming for:
(where clicking on tabs updates the url bar with the respective tab, and data that has been fetched in each tab is preserved when navigating back and forth)
@Tonkinese will check it out, thanks!
when will you try it?
TonkineseOP
literally just checking out a new branch now
@Tonkinesehow r things going?
@B33fb0n3 <@1124927091817844818>how r things going?
TonkineseOP
hey bro, ended up doing a bit of a refactor of my app using next v15 / Suspense, I've managed to solve the unnecessary re-rendering issue, but have yet to re-implement by tabs component (along with the updating of query params / history using nuqs), so hopefully that doesn't break things when I try lol
TonkineseOP
if I wanted to change the path instead of using a query param, is this actually possible in next? e.g. instead of the foo tab changing to url to /somepage?tab=foo, it changes it to /somepage/foo
Answer
@Tonkinese already implemented 😉
Perfect 🙌 is your thread solved like that?
TonkineseOP
yeah I think so, parallel routes solves the "query params" (not really but the url part) issue and these other features of app router and react v18 solve those other issues around components re-rendering unnecessarily, so all good!
thanks again for the advice dude 👍