国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

NextJS 13 更新伺服器元件數(shù)據(jù)
P粉015402013
P粉015402013 2023-12-23 12:02:07
0
1
651

所以我正在學(xué)習(xí)/適應(yīng) NextJS 13 及其新的 APP 資料夾。我很難弄清楚如何在無(wú)需重新加載頁(yè)面/應(yīng)用程式的情況下更新伺服器元件上的資料。目前,我的主頁(yè)伺服器元件從 MongoDB 取得項(xiàng)目(事件)清單。如果我在後臺(tái)更改資料庫(kù)數(shù)據(jù),然後導(dǎo)航到應(yīng)用程式上的另一條路線並返回主頁(yè)而不重新加載,則不會(huì)顯示新數(shù)據(jù)。只有當(dāng)我直接從瀏覽器硬重新加載頁(yè)面時(shí)才會(huì)顯示它。我的 EventDetails 元件也是如此。如果我在瀏覽器中加載應(yīng)用程式後直接在資料庫(kù)中更改事件的資料之一,則當(dāng)我導(dǎo)航到該事件的詳細(xì)資訊時(shí),該更改不會(huì)顯示在應(yīng)用程式中,除非我直接重新加載頁(yè)面。

我在取得函數(shù)中設(shè)定了以下選項(xiàng)

catch: 'no-cache'
next: { revalidate: 1 },

也嘗試了在元件檔案中匯出的設(shè)定

export const revalidate = 0;
export const dynamic = 'force-dynamic'

;

但它仍然沒(méi)有更新值。

這是我的完整程式碼。

// HomePage Component (app/page.jsx)
import React from 'react';
import MeetupList from './_components/meetups/meetupList';
export const revalidate = 0;
export const dynamic = 'force-dynamic';

const fetchMeetups = async () => {
  const response = await fetch('http://localhost:3000/api/meetup', {
    catch: 'no-cache',
    next: { revalidate: 1 },
  });

  const data = await response.json();

  return data;
};

const HomePage = async () => {
  const meetups = await fetchMeetups();
  return (
    <>
      <MeetupList meetups={meetups} />
    </>
  );
};

export default HomePage;


//MeetupList.jsx
import MeetupItem from './MeetupItem';

import styles from './MeetupList.module.css';
export const dynamic = 'force-dynamic';

function MeetupList({ meetups }) {
  return (
    <ul className={styles.list}>
      {meetups.map((meetup) => (
        <MeetupItem
          key={meetup._id}
          id={meetup._id}
          image={meetup.image}
          title={meetup.title}
          address={meetup.address}
        />
      ))}
    </ul>
  );
}

export default MeetupList;

這是否與我認(rèn)為仍處?kù)?Beta 模式的新伺服器操作有關(guān)?

謝謝

國(guó)際消費(fèi)電子展

P粉015402013
P粉015402013

全部回覆(1)
P粉463811100

顯然,這是 Next 13 中長(zhǎng)期存在的問(wèn)題。請(qǐng)參閱以下可追溯至 2022 年 11 月的票證,並隨時(shí)投票。

https://github.com/vercel/next.js/issues/42991

您可以在上面的線程中找到許多解決方法(儘管其中一些不再有效)。請(qǐng)查看哪一種最適合您的情況,但這是我目前使用的解決方法:

// components/Link.tsx

"use client";

import { ComponentProps, forwardRef } from "react";
import NextLink from "next/link";
import { useRouter } from "next/navigation";

export default forwardRef<
  HTMLAnchorElement,
  Omit<ComponentProps<typeof NextLink>, "href"> & {
    href: string;
    refresh?: boolean;
  }
>(function Link({ href, onClick, refresh, children, ...rest }, ref) {
  const router = useRouter();

  return refresh ? (
    <a
      ref={ref}
      href={href}
      onClick={(event) => {
        onClick?.(event);
        if (!event.defaultPrevented) {
          event.preventDefault();
          router.push(href);
          router.refresh();
        }
      }}
      {...rest}
    >
      {children}
    </a>
  ) : (
    <NextLink ref={ref} href={href} onClick={onClick} {...rest}>
      {children}
    </NextLink>
  );
});
// test/page.tsx

import Link from "@/components/Link";

<Link refresh href="/">Home</Link>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板