更改选项卡后,如何在React组件中维护iframe的状态?

发布于 2025-02-03 10:53:55 字数 1182 浏览 1 评论 0 原文

这是我正在使用的准则示例:

import { Tab } from "@headlessui/react";

export default function Tabs() {
  return (
    <Tab.Group>
      <Tab.List>
        <Tab>Tab 1</Tab>
        <Tab>Tab 2</Tab>
        <Tab>Tab 3</Tab>
      </Tab.List>
      <Tab.Panels>
        <Tab.Panel>Content 1</Tab.Panel>
        <Tab.Panel>Content 2</Tab.Panel>
        <Tab.Panel>{renderIframe()}</Tab.Panel>
      </Tab.Panels>
    </Tab.Group>
  );

  function renderIframe() {
    return (
      <iframe
        src="https://en.wikipedia.org/wiki/Main_Page"
        height="800px"
        width="800px"
        title="Example"
        loading="eager"
      ></iframe>
    );
  }
}

如果您单击“选项卡3”,则应看到一个指向Wikipedia的iframe。但是,如果您导航到IFRAME(例如“当前事件”)中的另一个页面,然后单击选项卡1,然后再次单击“选项卡3”,IFRAME将再次加载Wikipedia主页页面。我有没有办法维护此iframe的状态,以便即使在更改选项卡之后,它也与我返回Tab 3时完全相同?

Here's a barebones example of what I'm working with:

https://codesandbox.io/s/peaceful-faraday-xl3kz0?file=/src/Tabs.js

import { Tab } from "@headlessui/react";

export default function Tabs() {
  return (
    <Tab.Group>
      <Tab.List>
        <Tab>Tab 1</Tab>
        <Tab>Tab 2</Tab>
        <Tab>Tab 3</Tab>
      </Tab.List>
      <Tab.Panels>
        <Tab.Panel>Content 1</Tab.Panel>
        <Tab.Panel>Content 2</Tab.Panel>
        <Tab.Panel>{renderIframe()}</Tab.Panel>
      </Tab.Panels>
    </Tab.Group>
  );

  function renderIframe() {
    return (
      <iframe
        src="https://en.wikipedia.org/wiki/Main_Page"
        height="800px"
        width="800px"
        title="Example"
        loading="eager"
      ></iframe>
    );
  }
}

If you click on tab 3, you should see an iframe pointed at Wikipedia. However, if you navigate to another page within the iframe ('Current events', for example), and then click on tab 1, and then click on tab 3 again, the iframe will load the Wikipedia home page again. Is there a way for me to maintain the state of this iframe so that even after I change tabs, it's exactly the same as it was before when I go back to tab 3?

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

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

发布评论

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

评论(1

酷到爆炸 2025-02-10 10:53:55

您可能需要一个自定义事件侦听器,该侦听器将最后一个单击的URL存储在某些状态或cookie中(如果您想在刷新之间持续存在),那么在iFrame中,您可以做

src = {lasturl || 'https://en.wikipedia.org/wiki/main_page'}

一种替代的hacky方法可以 与z-index在一起,以便该选项卡3实际上从未从中删除。屏幕,但仅坐在CSS隐藏的背景中(尚未对此进行测试)。但是,这种方法可能会引起某种性能问题。

It doesn't look like there's a standard way of doing it, judging from this answer you may need a custom event listener that stores the last clicked url in some state or a cookie (if you want to persist between refreshes), then in your iFrame you can do

src={lastUrl || 'https://en.wikipedia.org/wiki/Main_Page'}

An alternative hacky approach could be to mess around with z-index so that tab 3 never actually is removed from the screen, but just sitting in the background hidden by CSS (haven't tested this). But there may be some performance concerns with this approach should it work.

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