next.js 如果使用了router.refresh(),则挂起回退不显示

ruyhziif  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

当我单击该组件时,所有组件都会重新呈现数据。
我的页面.jsx

const Home = () => {
  return(

   <RefreshBtn />
     <Suspense fallback={<NoteSkeleton />}>
        <Feed />
      </Suspense>
  )
}
export default Home

字符串
RefreshBtn组件

'use client'
import { useRouter } from 'next/navigation'

async function RefreshBtn () {
    const router = useRouter();

      const handleBtn = async (e) => {
        router.refresh();
        }

    return (
        <button onClick={handleBtn}>
            Refresh
        </button>
    );
};

export default RefreshBtn;


馈电元件SSR

import PromptCard from './PromptCard';

// use to fetch data from api
async function fetchPosts() {
    try{
        const response = await fetch('http://localhost:3000/api/prompt', { cache: 'no-store' });
        const data = await response.json();
        const posts = data;
        return posts;
    } catch(error) {
        console.log(error);
    }
}

// render the Feed component
async function Feed() {
    const posts = await fetchPosts();
    console.log(posts);
    return (

      <div className='mt-16 prompt_layout'>

        {posts.map((post) => (
          <PromptCard
            key={post._id}
            post={post}
          />
        ))}

      </div>
    );
}

export default Feed;


我想显示回退,但如果单击RefreshBtn,它会更新数据并重新获取所有内容,但不显示回退

owfi6suc

owfi6suc1#

我发现的一个解决方法是有一个在刷新时更改的键,以便Next知道内容已经更改:
Home.jsx

import { randomUUID } from 'crypto';

const Home = () => {
  return(

   <RefreshBtn />
     <Suspense fallback={<NoteSkeleton />}>
        <Feed />
      </Suspense>
  )
}
export default Home

字符串

相关问题