Arthur S
Arthur S

Reputation: 337

TabBarIOS - Is there a viewDidAppear or viewWillAppear equivalent?

Specifically for writing a callback from within a child component of a TabBarIOS.item that is triggered when a Tab is selected. (TabBarIOS in React Native)

Upvotes: 1

Views: 503

Answers (1)

while1
while1

Reputation: 3370

There is no callbacks for ViewDidAppear and ViewWillAppear for tabs. You can pass a prop like isTabAcitve in TabIOSItem child component. And implement your child component depending upon isTabActive value. Your code TabBarIOSItem can look something like this:

<TabBarIOS.Item
      title=""
      selected={this.isTabActive("my-account-tab")}
      icon={require("./img/user-tab.png")}
      onPress={() => {
        this.setState({activeTab:"my-account-tab"});
      }}>
      <MyAccountTabisTabActive={this.isTabActive("my-account-tab")} />
</TabBarIOS.Item>

isTabActive can be a method in your component containing TabIOS:

isTabActive(tabName)
{
   return this.state.activeTab == tabName;
}

Also TabBarIOS don't render all tabs at once, A TabBarIOS child component is initialised and mounted for first time only when that tab is pressed for first time. So componentWillMount and componentDidMount can also be used as alternative of viewDidAppear and viewWillAppear.

Upvotes: 2

Related Questions