Skip to content
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

Support mountOnEnter in Tab component #1552

Open
ismaildervisoglu opened this issue Jul 2, 2019 · 7 comments
Open

Support mountOnEnter in Tab component #1552

ismaildervisoglu opened this issue Jul 2, 2019 · 7 comments

Comments

@ismaildervisoglu
Copy link

  • components: tab
  • reactstrap version: 7.1.0
  • react version 16.8.4

Does TabPane support mountOnEnter props. If not support can you add this feature?

@epeterson320
Copy link

To add some context: This supports a use case I have where some responsive visualizations use the width of their container to lay out their content. If the tab content mounts before the tab is selected (when it is hidden), the width is calculated to be zero and the visualization doesn't re-render when the tab is focused, so the tab content area appears blank (its child is a component 0px wide).

@melissight
Copy link

I would love this feature as well. I have API calls that I do not want to run until the new tab is selected.

@TheSharpieOne
Copy link
Member

Sure, this can be an optional prop, when mountOnEnter is true it will mount and unmount the component based on the visibility of the tab.

For now the workaround is to control mounting yourself similar to:

import React, { useState } from 'react';
import { TabContent, TabPane, Nav, NavItem, NavLink, Card, Button, CardTitle, CardText, Row, Col } from 'reactstrap';
import classnames from 'classnames';

const Example = (props) => {
  const [activeTab, setActiveTab] = useState('1');

  const toggle = tab => {
    if(activeTab !== tab) setActiveTab(tab);
  }

  return (
    <div>
      <Nav tabs>
        <NavItem>
          <NavLink
            className={classnames({ active: activeTab === '1' })}
            onClick={() => { toggle('1'); }}
          >
            Tab1
          </NavLink>
        </NavItem>
        <NavItem>
          <NavLink
            className={classnames({ active: activeTab === '2' })}
            onClick={() => { toggle('2'); }}
          >
            Moar Tabs
          </NavLink>
        </NavItem>
      </Nav>
      <TabContent activeTab={activeTab}>
        <TabPane tabId="1">
          {activeTab === '1' && <Row>
            <Col sm="12">
              <h4>Tab 1 Contents</h4>
            </Col>
          </Row>}
        </TabPane>
        <TabPane tabId="2">
          {activeTab === '2' && <Row>
            <Col sm="6">
              <Card body>
                <CardTitle>Special Title Treatment</CardTitle>
                <CardText>With supporting text below as a natural lead-in to additional content.</CardText>
                <Button>Go somewhere</Button>
              </Card>
            </Col>
            <Col sm="6">
              <Card body>
                <CardTitle>Special Title Treatment</CardTitle>
                <CardText>With supporting text below as a natural lead-in to additional content.</CardText>
                <Button>Go somewhere</Button>
              </Card>
            </Col>
          </Row>}
        </TabPane>
      </TabContent>
    </div>
  );
}

export default Example;

@jomarquez21
Copy link

I think that your workaround isn't right because this would activate the "componentDidMount" life cycle as long as the activeTab is equal to the index.

@TheSharpieOne
Copy link
Member

@jomarquez21 isn't that what this issue is asking for? Have the component mount and thus "activate" the componentDidMount life-cycle when it becomes active (when the activeTab is equal to the index)?

@shehan-sandaruwan
Copy link

shehan-sandaruwan commented Oct 22, 2022

What if we only render active tab content, would it resolve that issue ..?
image
instead of this, we can have
image

LMK your thoughts on this

@JaiminR28
Copy link

Is this issue still active ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants