Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

475
Views
How can I maintain the state of an iframe in a React component after changing tabs?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!