English 中文(简体)
如何在下一个13个数据发生变化时重新提交数据?
原标题:How do re-render my data when it s changed in next 13?

How do I go about re-rendering my already fetched data in next-13 when passed as props? I m trying to get tasks.data which is fetched in the layout to rerender (useEffect?) inside of my TaskList component when I send a PUT request from another component.

排位:

import TaskList from "@/components/tasks/TaskList";
import SetTaskForm from  @/components/tasks/SetTaskForm ;
import { getAllTasks } from "@/utils/actions";

export default async function RootLayout({
  children
}: {
  children: React.ReactNode;
}) {
  const tasks = await getAllTasks()
  return (
    <html lang= en >
      <body>
        <main>{children}
          <SetTaskForm />
          <TaskList tasks={tasks} />
        </main>
      </body>
    </html>
  );
}

专题:

"use client"
import { TasksProps } from "@/types/taskTypes"
import Task from "./Task"
import { Suspense, useEffect } from "react";

export default function TaskList ({ tasks }: TasksProps) {
  useEffect(() => {

  }, [tasks])
  return (
    <section className="py-9 flex flex-col justify-center">
      <h2 className="text-xl font-semibold mt-6 border-b pb-1 text-center">Tasks</h2>
      <Suspense fallback={<div>Loading...</div>}>
        <ul className="flex flex-col mx-3">
          {tasks?.data?.map((task: TaskProps) => (
            <li key={task.id}>
              <Task 
                key={task._id}    
                title={task.title} 
                body={task.body} 
                bgColor={task.bgColor}
                isCollapsed={task.isCollapsed}
                isCompleted={task.isCompleted}
              />
            </li>
          ))}
        </ul>
      </Suspense>
    </section>
  )
}

任务:

"use client" 

import { deleteTask, toggleCollapseTask, toggleCompletedTask } from "@/utils/actions";
import { TaskProps } from "@/types/taskTypes";
import  material-icons/iconfont/material-icons.css 
import { useState } from "react";

export default function Task({ id, title, body, isCollapsed, bgColor, isCompleted }: TaskProps) {
  const [collapsed, setCollapsed] = useState(isCollapsed)
  const [completed, setCompleted] = useState(isCompleted)
  const handleCollapse = () => {
    setCollapsed(!collapsed)
    toggleCollapseTask(title, collapsed)
  }
  const handleCompleted = () => {
    setCompleted(!completed)
    toggleCompletedTask(title, completed)
  }
  return (
    <section>
      <div>
        <h1>{title}</h1>
        <span 
          onClick={e => deleteTask(title)}
        >delete</span>
      </div>
      <div>
        <span onClick={handleCollapse}>expand_more</span>
      {!collapsed && <a>{body}</a>}
      </div>
    </section>
  );
}

路线:

import { PrismaClient } from "@prisma/client";
import { NextResponse}  from "next/server"

const prisma = new PrismaClient()

export async function GET(request: Request) {
  const data = await prisma.tasks.findMany()
  return NextResponse.json({ data })
}

行动:

"use server";

export async function getAllTasks() {
  const res = await fetch("http://localhost:3000/api/getAllTasks"
  );
  if (!res.ok) {
    throw new Error("Failed to fetch data");
  }
  return res.json();
}

我的法典的任何帮助或暗示都受到高度赞赏。

问题回答

如果推进或国家改变,反应将自动进行。 但问题是下一个问题。 j 13个数据库按违约计算。 页: 1

export async function getAllTasks() {
  const res = await fetch("http://localhost:3000/api/getAllTasks",
   {
         cache:  no-store ,    
   }
  );
  if (!res.ok) {
    throw new Error("Failed to fetch data");
  }
  return res.json();
}




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签