Hi,
I am unable to set active tab dynamically. Please provide solution for this problem. I am sharing required information below.
N/A
N/A
N/A
N/A
Not a bug
N/A
@deepupathak Check the docs for Tabs
initialPage should do the work
Pass the page number to this
Hi @SupriyaKalghatgi thanks for quick response.
I have tried this but doesn't work for me. I am sharing code snippet below -
activateTab() {
if (this.state.Option == 0) {
this.setState({ activeTabValue: 0 });
} else
this.setState({ activeTabValue: 1 });
}
and I am callingactivateTab() method on button click and pass this.state.activeTabValue inside initialPage.
@deepupathak Did you try passing static values to initialPage?
Did that work?
@SupriyaKalghatgi Its working but I want to open the tab on button click with dynamic tab number (activeTabValue) which I mentioned above.
@SupriyaKalghatgi waiting for your response.
For your convenience, I am providing code sample more specifically.
<Button onPress={() => this.activateTab()}>
<Text>SET</Text>
</Button>
activateTab() {
if (this.state.Option == 0) {
this.setState({ activeTabValue: 0 });
} else
this.setState({ activeTabValue: 1 });
}
<Tabs initialPage={this.state.activeTabValue}>
<Tab heading={<TabHeading><Text>Tab 1</Text></TabHeading>}>
<Text>First tab</Text>
</Tab>
<Tab heading={<TabHeading><Text>Tab 2</Text></TabHeading>}>
<Text>Second tab</Text>
</Tab>
</Tabs>
@deepupathak you can use page prop of <Tabs/> component. Pasting a sample code
Code
import React, { Component } from 'react';
import { Container, Header, Content, Tab, Tabs, TabHeading, Text, Button } from 'native-base';
export default class TabsExample extends Component {
constructor(props) {
super(props)
this.state = { initialPage: 1, activeTab: 1 }
}
render() {
return (
<Container>
<Header hasTabs />
<Tabs initialPage={this.state.initialPage} page={this.state.activeTab}>
<Tab heading={<TabHeading><Text>Tab 1</Text></TabHeading>}>
<Text>First tab</Text>
<Button style={{ margin: 10 }} onPress={() => this.setState({ activeTab: 1 })}><Text>Go to second tab</Text></Button>
</Tab>
<Tab heading={<TabHeading><Text>Tab 2</Text></TabHeading>}>
<Text >Second tab</Text>
<Button style={{ margin: 10 }}><Text onPress={() => this.setState({ activeTab: 0 })}>Go to first tab</Text></Button>
</Tab>
</Tabs>
</Container>
);
}
}
Gif

@akhil-geekyants, thanks! Is there a reason why this isn't mentioned in the docs?
Thank you, this is helpful. How would I know which tab is active if the user click on it (instead of me doing so programmatically?
@akhil-geekyants thank you for your help.
@akhil-geekyants Thanks. It could be added to docs!
Thank you, this is helpful. How would I know which tab is active if the user click on it (instead of me doing so programmatically?
@dchersey did you find any solution yet ?
@Abhishekgarg727
I tried using the onChange attribute to track the selected tab. However, using this value to set the page was proving to be difficult. Instead, I found a goToPage method on the Tabs component. So I created a ref and called that method on button presses.
import React, { Component } from 'react';
import { Container, Header, Content, Tab, Tabs, TabHeading, Text, Button } from 'native-base';
export default class TabsExample extends Component {
constructor(props) {
super(props)
this._tabs = null;
}
render() {
return (
<Container>
<Header hasTabs />
<Tabs ref={component => this._tabs = component}>
<Tab heading={<TabHeading><Text>Tab 1</Text></TabHeading>}>
<Text>First tab</Text>
<Button style={{ margin: 10 }} onPress={() => this._tabs.goToPage(1);}><Text>Go to second tab</Text></Button>
</Tab>
<Tab heading={<TabHeading><Text>Tab 2</Text></TabHeading>}>
<Text >Second tab</Text>
<Button style={{ margin: 10 }}><Text onPress={() => this._tabs.goToPage(0);}>Go to first tab</Text></Button>
</Tab>
</Tabs>
</Container>
);
}
}
Now, it doesn't matter if the user presses the button or the tab, the internal state of the tabs isn't mucked up. If you still need to track what the current active tab is, you can then use:
...
<Tabs ref={component => this._tabs = component} onChange={({i}) => this.setState({ currentPage: i })}>
...
How to know on which tab we are present? any method for that.
@deepupathak you can use
pageprop of<Tabs/>component. Pasting a sample codeCode
import React, { Component } from 'react'; import { Container, Header, Content, Tab, Tabs, TabHeading, Text, Button } from 'native-base'; export default class TabsExample extends Component { constructor(props) { super(props) this.state = { initialPage: 1, activeTab: 1 } } render() { return ( <Container> <Header hasTabs /> <Tabs initialPage={this.state.initialPage} page={this.state.activeTab}> <Tab heading={<TabHeading><Text>Tab 1</Text></TabHeading>}> <Text>First tab</Text> <Button style={{ margin: 10 }} onPress={() => this.setState({ activeTab: 1 })}><Text>Go to second tab</Text></Button> </Tab> <Tab heading={<TabHeading><Text>Tab 2</Text></TabHeading>}> <Text >Second tab</Text> <Button style={{ margin: 10 }}><Text onPress={() => this.setState({ activeTab: 0 })}>Go to first tab</Text></Button> </Tab> </Tabs> </Container> ); } }Gif
how to do the same functionality by swiping left or right without onPress?
@deepupathak you can use
pageprop of<Tabs/>component. Pasting a sample code
Codeimport React, { Component } from 'react'; import { Container, Header, Content, Tab, Tabs, TabHeading, Text, Button } from 'native-base'; export default class TabsExample extends Component { constructor(props) { super(props) this.state = { initialPage: 1, activeTab: 1 } } render() { return ( <Container> <Header hasTabs /> <Tabs initialPage={this.state.initialPage} page={this.state.activeTab}> <Tab heading={<TabHeading><Text>Tab 1</Text></TabHeading>}> <Text>First tab</Text> <Button style={{ margin: 10 }} onPress={() => this.setState({ activeTab: 1 })}><Text>Go to second tab</Text></Button> </Tab> <Tab heading={<TabHeading><Text>Tab 2</Text></TabHeading>}> <Text >Second tab</Text> <Button style={{ margin: 10 }}><Text onPress={() => this.setState({ activeTab: 0 })}>Go to first tab</Text></Button> </Tab> </Tabs> </Container> ); } }Gif
how to do the same functionality by swiping left or right without onPress?
g.i will give u the active index
Most helpful comment
@deepupathak you can use
pageprop of<Tabs/>component. Pasting a sample codeCode
Gif