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

Load content of tab when pressing tab instead load all tabs #2331

Closed
SoldierCorp opened this issue Sep 3, 2017 · 16 comments
Closed

Load content of tab when pressing tab instead load all tabs #2331

SoldierCorp opened this issue Sep 3, 2017 · 16 comments

Comments

@SoldierCorp
Copy link

Version

Tell us which versions you are using:

  • react-native-router-flux v4.0.0-beta.21
  • react-native v0.46.4

Expected behaviour

  • Load content when the user touch the tab.

Actual behaviour

  • All the content of my 3 tabs are being loaded at the same time. I have an API request within componentDidMount() function on each one so sometimes its slow.
@Nautal
Copy link

Nautal commented Sep 4, 2017

have you tried the "lazy" property ?

@mcabs3
Copy link
Collaborator

mcabs3 commented Sep 5, 2017

@SoldierCorp currently I don't believe there is way to render the tab "new" every time you go to the tab, if you are looking for just when you initially go to a tab for the first time, then @Nautal has the right idea of using the lazy property on the tabs you want to lazy load.

@pgonzalez-santiago
Copy link

@SoldierCorp Did you find any solution? I am having the same problem, I want to fetch the data of a second tab only when this tab is active.

@SoldierCorp
Copy link
Author

@Nautal I cannot see any difference using lazy

@mcabs3 I want to know if there is a way to detect the tab event to compare if the content has not been loaded, then load the content because I'm using axios to get data from my API

@pgonzalez-santiago Not yet u.u

@aksonov
Copy link
Owner

aksonov commented Sep 6, 2017 via email

@pgonzalez-santiago
Copy link

@SoldierCorp I added the prop "lazy" in the tabs parent and it works. Fetch data in the componentDidMount() on my second tab is called when I navigate into it. I think this is what you want.

@SoldierCorp
Copy link
Author

@aksonov I'll check how to use it, thank you!

@pgonzalez-santiago That's weird, I have that prop and componentDidMount but all of them are being loaded at the same time.

@pgonzalez-santiago
Copy link

@SoldierCorp Could you paste the code of your scenes/tabs please?

@SoldierCorp
Copy link
Author

<Router createReducer={reducerCreate}>
  <Stack key='main'>
    <Scene key='contact' hideNavBar='true' backTitle={'Regresar'} component={Contact} passProps={true}
      openSideMenu={props.openSideMenu}
      closeSideMenu={props.closeSideMenu}
      type={ActionConst.REPLACE}
    />
    <Scene key='gifts' hideNavBar='true' backTitle={'Regresar'} component={Gifts} passProps={true}
      openSideMenu={props.openSideMenu}
      closeSideMenu={props.closeSideMenu}
      type={ActionConst.REPLACE}
    />

    <Scene key='team' hideNavBar='true' backTitle={'Regresar'} component={Team} passProps={true}
      openSideMenu={props.openSideMenu}
      closeSideMenu={props.closeSideMenu}
      type={ActionConst.REPLACE}
    />

    <Scene  key='business' component={Business} title='INFORMACIÓN' hideNavBar='true' passProps={true} />

    <Scene key='filter' component={Filter} type={ActionConst.PUSH} passProps={true} navigationBarStyle={navBar}
      title={'Filtros de búsqueda'}
      titleStyle={titleStyle}
      leftButtonImage={ require('./assets/images/icons/close_icon.png') }
      onLeft={ Actions.pop }
    />
    
    {/* Tabs */}

    <Tabs
      key="home"
      tabs
      tabBarStyle={styles.tabBarStyle}
      hideNavBar="true"
      showLabel={false}
      tabBarPosition='bottom'
      titleStyle={titleStyle}
      navigationBarStyle={navBar}
      onLeft={props.openSideMenu}
      onRight={() => Actions.filter()}
      rightButtonTextStyle={filterTextStyle}
      hideBackImage={true}
      leftButtonImage={ require('./assets/images/icons/menu_trigger.png') }
      rightButtonImage={ require('./assets/images/icons/filter_icon.png') }
      activeBackgroundColor="#262a62"
      inactiveBackgroundColor="#444881"
    >
      <Scene
        key='info'
        component={BusinessList}
        icon={TabIcon}
        openSideMenu={props.openSideMenu}
        type={ActionConst.REPLACE}
        title="🏬 Negocios"
      />
      <Scene
        key="featured"
        component={Featured}
        title="♥ Destacados"
        icon={TabIcon}
        passProps={true}
        lazy
      />
      <Scene
        key="events"
        component={Events}
        title="🎉 Eventos"
        icon={TabIcon}
        passProps={true}
        lazy
      />
    </Tabs>
  </Stack>
</Router>

@aksonov
Copy link
Owner

aksonov commented Sep 10, 2017

lazy must be defined for Tabs, not for scene, please carefully check docs...

@aksonov aksonov closed this as completed Sep 10, 2017
@SoldierCorp
Copy link
Author

Actually if you check the docs, lazy is in the Scene not in Tabs, please review that part https://github.com/aksonov/react-native-router-flux/blob/master/docs/API.md

@hugoh59
Copy link

hugoh59 commented Jan 5, 2018

I've set lazy="true" in my scene but it still loads the componentDidMount method. How can we fix this?

@aksonov
Copy link
Owner

aksonov commented Jan 5, 2018

Read docs carefully.

@hugoh59
Copy link

hugoh59 commented Jan 5, 2018

Ok, sorry about that I found a fix! The lazy attribute is in fact working. My problem was coming from the fact that I was calling a onPress={this.connectWithFacebook()} instead of onPress={() => this.connectWithFacebook()} which was calling the function automatically. If this can help anyone in the future. Sorry about that!

@amitbravo
Copy link

@hugoh1995 so , is "lazy" a solution to get rid of this problem ? I have similar problem , my app is like land on signin page first and then after logged in user lands to 4tabs e.g. news , photos , videos and other , when user successfully logged in , it takes around 3-4 seconds pause from landing signin page to tab group because big heap of data is fetched , renders along with images , so I want user when he lands on main tabs which is news only this data should be loaded , rest of other 3 tabs data should get whenever we hit on them.

@Fiaz-Tariq
Copy link

Fiaz-Tariq commented Nov 10, 2023

Lazy Loading Tab Content in React Bootstrap:

Implementing lazy loading for tab content in React Bootstrap allows you to load the content of a specific tab only when that tab is clicked. This approach optimizes performance by avoiding the upfront loading of all tab content at once (avoid all tabs API response). Utilizing conditional rendering based on the active tab ensures that only the selected tab's content is displayed, providing a more efficient and responsive user experience. The example code demonstrates how to achieve lazy loading using the Tabs and Tab components in React:

const [activeTab, setActiveTab] = useState('tab1');
const handleTabSelect = (selectedTab) => {
setActiveTab(selectedTab);
};

<Tabs
      id="controlled-tab-example"
      activeKey={activeTab}
      onSelect={handleTabSelect}
      className="mb-4 pb-2 custom-tabs">
      
      <Tab eventKey="tab1" title="Tab 1 ">
        {activeTab === 'tab1' && <TabOneContent />}
      </Tab>
      <Tab eventKey="tab2" title="Tab 2">
        {activeTab === 'tab2' && <TabTwoContent />}
      </Tab>
 </Tabs>

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

No branches or pull requests

8 participants