-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance issue: inactive tabs should not be rendered #3375
Comments
While I agree there can be performance implications where each tab's For what it's worth, this behavior is largely attributed to extending () => {
const [key, setKey] = useState('home');
return (
<Tabs
id="controlled-tab-example"
activeKey={key}
onSelect={(k) => setKey(k)}
>
<Tab eventKey="home" title="Home">
{key === 'home' && (
<>
Hello I am the first panel.
{console.log('first panel')}
</>
)}
</Tab>
<Tab eventKey="profile" title="Profile">
{key === 'profile' && (
<>
Hello I am the second panel.
{console.log('second panel')}
</>
)}
</Tab>
<Tab eventKey="contact" title="Contact" disabled>
{key === 'contact' && (
<>
Hello I am the third panel.
{console.log('third panel')}
</>
)}
</Tab>
</Tabs>
);
} That said, I agree there's some risk this approach is not implemented in cases where it really should be. I wonder if we'd be able to implement something like the proposed conditional rendering (e.g., based on an opt-in prop like
I'm not sure
See below Paragon Playground example of the resulting DOM structure for Regarding the interaction with a router, though, I definitely 100% agree As for prior art, there are a few instances where we integrate React Router with
|
Feedback summary
As far as I can tell, Paragon currently processes and renders the entire React component tree for all tabs in a
<Tabs>
component. This can cause poor performance when tabs are used for major parts of the application, not only because of unnecessary rendering but can also cause unnecessary data loading and API calls if the inactive tabs have those.Here is a simple example showing the issue. No messages should be logged to the console, but two messages are logged from the contents of the inactive tab:
Improved API
Generally, the way to fix this is to make each
<Tab>
accept the child component as a prop, and not as JSXchildren
. Then it can be conditionally rendered.i.e.
<Tab><Child /></Tab>
becomes<Tab component={Child} />
Simple Workaround
Alternately, the docs could be updated to say that
<Tabs>
should only be used for "minor" parts of an application, like two different views within a modal, and that larger chunks of the application should strictly use<Nav variant="tabs">
(which is available in Paragon) together with a router to control the "tab pane" content. At the moment, we don't seem to document nor encourage this pattern. The only openedx example I could find of using<Nav variant="tabs'>
is not using a client-side router.So, I don't have a working Paragon example handy (even though I think it's possible), but here is a working example from another application I made, using Catalyst Navbar as the Tabs component and wouter as the routing framework. In this example, only the active tab is rendered.
The text was updated successfully, but these errors were encountered: