如何重新验证与SWR关闭反应模式的数据?

发布于 2025-02-11 04:31:02 字数 1663 浏览 3 评论 0原文

我正在尝试重新验证 reaction-modal 在NextJS项目中使用SWR关闭。

我正在使用swr挂钩。


  const { data, error, isValidating } = useSWR(
    process.env.NEXT_PUBLIC_APP_URL + `/api/users`,
    fetcher,{
    revalidateIfStale: true,
    revalidateOnFocus: true,
    revalidateOnMount:true, 
   }
  );


  useEffect(() => {
    if (data) {
      setUsers(data.users);
    }
  }, [data, isValidating, users]);

//handle loading..
//handle error..


  return (
    <main className="mx-auto max-w-7xl ">
      <Header title="All users"/>
      <UsersList users={users} />
    </main>
  );

我正在获取用户列表并显示它们。

  const usersList = users.((user) => (
    <div className="space-x-5 text-sm" key={user.id}>
      {user.name}
      <DisableModal id={user.id} isDisabled={user.active}/>
    </div>
  ));

我有一个React模式,一旦我用句柄单击禁用了用户,就可以使用户禁用用户。

当模态关闭时,数据没有被淘汰。 这是来自文档的样本模式。

当我关闭模式时,可以看到用户列表。它们没有刷新,也没有使用SWR使用重新验证。


export const DisableModal = ({
  id,
  isDisabled,
}) => {
  const [disableModalIsOpen, setDisableModalIsOpen] = useState(false);

  function closeDisableModal() {
    setDisableModalIsOpen(false);
  }

  function openPublishModal() {
    setDisableModalIsOpen(true);
  }

  const handleDisableUser = async () => {
  //disable logic in rest call.
   closeDisableModal();
  }
....
}

I am trying to revalidate the data on react-modal close using SWR in a NextJS project.

I am using the SWR hook like so.


  const { data, error, isValidating } = useSWR(
    process.env.NEXT_PUBLIC_APP_URL + `/api/users`,
    fetcher,{
    revalidateIfStale: true,
    revalidateOnFocus: true,
    revalidateOnMount:true, 
   }
  );


  useEffect(() => {
    if (data) {
      setUsers(data.users);
    }
  }, [data, isValidating, users]);

//handle loading..
//handle error..


  return (
    <main className="mx-auto max-w-7xl ">
      <Header title="All users"/>
      <UsersList users={users} />
    </main>
  );

I am fetching a list of users and displaying them.

  const usersList = users.((user) => (
    <div className="space-x-5 text-sm" key={user.id}>
      {user.name}
      <DisableModal id={user.id} isDisabled={user.active}/>
    </div>
  ));

I have a react modal that allows us to disable the users, once I have disabled the users with handle click.

When the modal closes the data is not being refetched.
This is a sample modal from the docs.

When I close the modal, and can see the list of users. They are not refreshed and not using revalidations with use SWR.


export const DisableModal = ({
  id,
  isDisabled,
}) => {
  const [disableModalIsOpen, setDisableModalIsOpen] = useState(false);

  function closeDisableModal() {
    setDisableModalIsOpen(false);
  }

  function openPublishModal() {
    setDisableModalIsOpen(true);
  }

  const handleDisableUser = async () => {
  //disable logic in rest call.
   closeDisableModal();
  }
....
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2025-02-18 04:31:02

您可以使用 突变 代码>在模态中触发模式中的回调。

export const DisableModal = () => {
    const [showModal, setShowModal] = useState(false);
    const { mutate } = useSWRConfig()

    return (
        <>
            <button onClick={() => { setShowModal(true) }}>Trigger Modal</button>
            <ReactModal 
                isOpen={showModal}
                onAfterClose={() => { 
                    mutate(process.env.NEXT_PUBLIC_APP_URL + '/api/users') 
                }}
                contentLabel="Minimal Modal Example"
            >
                <button onClick={() => { setShowModal(false) }}>Close Modal</button>
            </ReactModal>
        </>
    )
}

调用突变(process.env.next_public_app_url +'/api/用户')将使用给定的键向SWR挂钩向SWR挂载。意思是usesingwr(process.env.next_public_app_url +'/api/users',fetcher,{...})挂钩将重新运行并返回更新的用户数据。

You can revalidate the data manually using mutate when the onAfterClose callback in the modal gets triggered.

export const DisableModal = () => {
    const [showModal, setShowModal] = useState(false);
    const { mutate } = useSWRConfig()

    return (
        <>
            <button onClick={() => { setShowModal(true) }}>Trigger Modal</button>
            <ReactModal 
                isOpen={showModal}
                onAfterClose={() => { 
                    mutate(process.env.NEXT_PUBLIC_APP_URL + '/api/users') 
                }}
                contentLabel="Minimal Modal Example"
            >
                <button onClick={() => { setShowModal(false) }}>Close Modal</button>
            </ReactModal>
        </>
    )
}

Calling mutate(process.env.NEXT_PUBLIC_APP_URL + '/api/users') will broadcast a revalidation message to SWR hook with that given key. Meaning the useSWR(process.env.NEXT_PUBLIC_APP_URL + '/api/users', fetcher, { ... }) hook will re-run and return the updated users data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文