我成功运行了添加功能,但是当我签入列表时,它不想添加我之前添加的数据
import React, { useEffect, useState } from "react";
import { createPost } from "./actions";
import { SubmitButton } from "./submitButton";
async function getPosts() {
const res = await fetch("https://dummyjson.com/posts/search?q=&limit=0&skip=0", { cache: 'no-store' });
const {posts} = await res.json();
return posts;
}
const FormPost = async ({ params }: { params: { id: string } }) => {
const data = await getPosts()
return (
<>
<form action={createPost}>
<input type="text" name="title" className="border" />
<input type="text" name="body" className="border" />
<input type="text" name="userId" className="border" />
<input type="text" name="tags" className="border" />
<input type="text" name="reactions" className="border" />
<SubmitButton />
</form>
<div className="p-10 flex flex-wrap gap-5">
{data.map((d: any, index: any) => (
<div key={index} className="p-1 shadow">
<p>{d.body.slice(0, 5)}</p>
</div>
))}
</div>
</>
);
};
export default FormPost;
如 dummyjson 文档中所述,发出 POST 请求来添加帖子仅模拟响应。它不会将新帖子添加到数据库中,并且获取帖子列表只会返回默认帖子,而不是创建的帖子(因为它们不会添加到帖子数据库中)。