react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

Callback on switching tabs

Open wgormley opened this issue 10 years ago • 21 comments

is there anyway to know when a tab has become the active tab, or a way to set a callback on tab presses/changes?

wgormley avatar Apr 13 '16 22:04 wgormley

+1

apvlv avatar Jun 05 '16 19:06 apvlv

+1

gwendall avatar Jun 24 '16 14:06 gwendall

Actually this would be really good - I have a tab that opens up a photo gallery or camera (like instagram). I just really need to find out which is the active tab, I've done it before using JS only but I have decided that the only way forward is with react-native is with react-native-controllers/react-native-navigation (from Wix!!). I can wait.

willks avatar Jun 29 '16 14:06 willks

This feature is at the top of our list. Will be added soon, after the Android version matures a bit.

guyca avatar Jun 30 '16 05:06 guyca

+1 Desperately need this. Are there any current workarounds to get the active tab or when the tab presses/changes?

alfonsogacevedo avatar Sep 01 '16 22:09 alfonsogacevedo

+1

tylerclark avatar Sep 07 '16 01:09 tylerclark

+1

ppsreejith avatar Sep 29 '16 15:09 ppsreejith

+1

eggmantv avatar Oct 21 '16 07:10 eggmantv

+1

ccokme avatar Oct 21 '16 07:10 ccokme

Check out our branch here for a quick and easy implementation: https://github.com/3sidedcube/react-native-navigation/tree/feature/view_lifecycle

Happy to pull request it, but it's built on top of a fork that we're already waiting on a large pull request to go through on so will have to wait for that first!

simonmitchell avatar Nov 18 '16 15:11 simonmitchell

Any update on this guys?

Jaafar-abusair avatar Feb 09 '17 12:02 Jaafar-abusair

@Jaafar-abusair I believe the team at wix will be merging the branch that our view_lifecycle branch was built on top of fairly soon, once they have done so we'll submit a PR for this logic :) It's fairly small and simple to implement, so wouldn't be too long I guess :D

simonmitchell avatar Feb 09 '17 12:02 simonmitchell

I think this can also be achieved right now by using the bottomTabSelected event for bottom tabs and tabSelected event for top tabs. When the application is started, the first screen is the selected tab. Afterward, you can listen on one these two events depending on your layout to keep track of the currently selected tab.

varungupta85 avatar Feb 09 '17 12:02 varungupta85

Agreed, what we implemented was slightly different to what is being requested here to be fair, we implemented callbacks for all view lifecycle methods: viewWillAppear, viewDidAppear, viewWillDissapear, viewDidDissapear which gives more info than this, but you're correct this is achievable right now :)

simonmitchell avatar Feb 09 '17 12:02 simonmitchell

@varungupta85 how to listen to bottomTabSelected?

Jaafar-abusair avatar Feb 13 '17 09:02 Jaafar-abusair

@Jaafar-abusair https://github.com/wix/react-native-navigation/issues/648#issuecomment-271718783

guyca avatar Feb 13 '17 09:02 guyca

+1

uunc avatar Feb 19 '17 03:02 uunc

We added bottomTabSelected event which you can to globally. It returns the indices of the newly selected and the unselected tabs.

import {
  NativeAppEventEmitter,
  DeviceEventEmitter,
  Platform
} from 'react-native';

export default class TabSelectedListener {
  constructor(tabs) {
    this.tabs = tabs;
  }

  register() {
    const emitter = this.getEventEmitter();
    this.eventSubscription = emitter.addListener('bottomTabSelected', (event) => {
      console.log('tabSelected ', `selected: ${event.selectedTabIndex} unselected: ${event.unselectedTabIndex}`);
    });
  }

  unregister() {
    if (this.eventSubscription) {
      this.eventSubscription.remove();
    }
  }

  getEventEmitter() {
    return Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
  }
}

guyca avatar Apr 09 '17 15:04 guyca

It seems this issue can be closed by https://github.com/wix/react-native-navigation/issues/16#issuecomment-292791342.

Do we need to implement something else in the scope of it?

Kureev avatar Apr 24 '17 10:04 Kureev

@Kureev This is the final implementation for the current scope, so lets close the issue for now as it has been addressed. Thanks for sorting things out 👍

guyca avatar Apr 25 '17 06:04 guyca