Next.js Discord

Discord Forum

Data not refreshing when going back to previous page

Unanswered
SharpieMaster posted this in #help-forum
Open in Discord
"use client";
import { type Survey } from "@prisma/client";
import Link from "next/link";
import { useState, type FC } from "react";
import { cn } from "~/lib/utils";
import { buttonVariants } from "./ui/button";

interface SurveyListProps {
  defaultSurveys: Survey[];
}

const SurveyList: FC<SurveyListProps> = ({ defaultSurveys }) => {
  const [surveys, setSurveys] = useState<Survey[]>(defaultSurveys);
  return (
    <div className="mt-10 p-6">
      <h1 className="mb-5 pl-20 text-3xl">Your Surveys:</h1>
      <div className="mx-auto grid w-3/4 grid-cols-6 gap-2">
        {surveys.map((survey, index) => (
          <Link
            href={`/survey/${survey.id}`}
            className={cn(
              "col-span-1 gap-2 border",
              buttonVariants({ variant: "link" }),
            )}
          >
            {survey.title}
          </Link>
        ))}
      </div>
    </div>
  );
};

export default SurveyList;



import { redirect } from "next/navigation";
import Navbar from "~/components/Navbar";
import { getServerAuthSession } from "~/server/auth";
import NewSurveyDialog from "~/components/NewSurveyDialog";
import SurveyList from "~/components/SurveyList";
import { api } from "~/trpc/server";

const Account = async () => {
  const session = await getServerAuthSession();
  if (!session) {
    redirect("/");
  }

  const surveys = await api.survey.getAll.query();

  return (
    <>
      <Navbar />
      <main className="mx-auto w-2/3">
        <h1 className="p-20 pb-0 text-5xl text-zinc-400">
          Hello, <span className="italic">{session.user.name}</span>
        </h1>
        <SurveyList defaultSurveys={surveys} />
        <NewSurveyDialog />
      </main>
    </>
  );
};

export default Account;

6 Replies

American Chinchilla
Might be a side effect of soft navigation & the client side cache, state gets preserved:
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#5-soft-navigation

One possible workaround (I'm working through something very similar rn so this might not be the best approach, if somebody has something better please let me know) is to add a useEffect to your SurveyList component that uses router.refresh(). Similar to here:
https://youtu.be/25yjSzl6PsQ?si=qMZ0dqWCitJVW_nE&t=349

useEffect(() => {
  router.refresh()
}, [])
@SharpieMaster thanks, but im trying to keep the whole page server-side, I could re-fetch the data inside of the client component but that will still take time for it to update
American Chinchilla
I've been trying to do the same myself--the options are unfortunately pretty limited though
i mean, surely there must be some way to do it
@SharpieMaster i mean, surely there must be some way to do it
American Chinchilla
The router.refresh way is probably the cleanest if not using server actions
  const getSurveys = api.survey.getAll.useQuery();

  useEffect(() => {
    if (getSurveys.data) {
      setSurveys(getSurveys.data);
    }
  }, [getSurveys.data]);

I'll just add this untill i find a better way to do this