Next.js Discord

Discord Forum

Todo list not updating when using redirect

Answered
Eyebrowed Thrush posted this in #help-forum
Open in Discord
Eyebrowed ThrushOP
I am following this tutorial: https://www.youtube.com/watch?v=NgayZAuTgwM and for some reason the TodosList is not refreshing when I am adding a todo and clicking 'Create' button. I am getting redirected but I need to refresh the page to see new todo.

My component:

import { prisma } from "@/db";
import { redirect } from "next/navigation";
import Link from "next/link";

async function createTodo(data: FormData) {
  "use server";

  const title = data.get("title")?.valueOf();
  if (typeof title !== "string" || title.length === 0) {
    throw new Error("Invalid Title");
  }

  await prisma.todo.create({ data: { title, complete: false } });
  redirect("/");
}

export default function New() {
  return (
    <>
      <header className="flex justify-between itsm-center mb-4">
        <h1 className="text-3xl">New</h1>
      </header>
      <form action={createTodo} className="flex gap-2 flex-col">
        <input
          type="text"
          name="title"
          className="border border-slate-300 bg-transparent rounded px-2 py-1 outline-none focus-within:border-slate-100"
        />
        <div className="flex gap-1 justify-end">
          <Link
            className="border border-red-700 text-slate-300 px-2 py-1 rounded hover:bg-slate-700 focus-within:bg-slate-700 outline-none"
            href=".."
          >
            Cancel
          </Link>
          <button
            type="submit"
            className="border border-slate-300 text-slate-300 px-2 py-1 rounded hover:bg-slate-700 focus-within:bg-slate-700 outline-none"
          >
            Create
          </button>
        </div>
      </form>
    </>
  );
}

any help would be great thanks!
Answered by DirtyCajunRice | AppDir
you need to make sure its not being cached or is being revalidated
View full answer

8 Replies

Eyebrowed ThrushOP
here is also the component for the page im getting redirected to
 import { TodoItem } from "@/components/TodoItem";
import { prisma } from "@/db";
import Link from "next/link";

function getTodos() {
  return prisma.todo.findMany();
}

export default async function Home() {
  const todos = await getTodos();

  return (
    <>
      <header className="flex justify-between itsm-center mb-4">
        <h1 className="text-3xl">Todos</h1>
        <Link
          className="border border-slate-300 text-slate-300 px-2 py-1 rounded hover:bg-slate-700 focus-within:bg-slate-700 outline-none"
          href="/new"
        >
          New
        </Link>
      </header>
      <ul className="pl=4">
        {todos.map((todo) => (
          <TodoItem key={todo.id} {...todo} />
        ))}
      </ul>
    </>
  );
}
@Eyebrowed Thrush here is also the component for the page im getting redirected to
you need to make sure its not being cached or is being revalidated
Answer
@DirtyCajunRice | AppDir you need to make sure its not being cached or is being revalidated
Eyebrowed ThrushOP
Thanks ill look into it
@Eyebrowed Thrush Thanks ill look into it
do you want it to cache unless you add a todo?
@DirtyCajunRice | AppDir do you want it to cache unless you add a todo?
Eyebrowed ThrushOP
Yeh that would be preffered
@DirtyCajunRice | AppDir you need to make sure its not being cached or is being revalidated
Eyebrowed ThrushOP
thanks I got it working with revalidatePath
@Eyebrowed Thrush thanks I got it working with revalidatePath
Good on you for searching the docs properly! please mark solution if done 🙂