0

I'm using native-base, redux and react-native, It seems that switching between tabs doesn't call the method componentWillUnmount, I want to do some actions when the tab is switched and the component is removed:

Main.js

export class Main extends Component {
  render() {
    return (
    <Container style={styles.containerStyle}>
        <Tabs initialPage={0}  >
        <Tab heading="Tab1">
            <Tab1 />
          </Tab>
          <Tab heading="Tab2">
            <Tab2 />
          </Tab>
        </Tabs>
      </Container>
    )
  }
}

Tab1.js

export class Tab1 extends Component {

  constructor(props) {
    super(props);
}

  componentWillMount(){   
    //called 
  }

  componentWillUnmount(){
    //not called when switching tabs
  }


  render() {
    return (
      <Container style={styles.containerStyle}>
        <Content>
          <Text>Tab1</Text>
        </Content>
    </Container>
    );
  }
}

How to call actions when the tab is switched and the component is removed?

Vahid Boreiri
  • 3,418
  • 1
  • 19
  • 34
Mahmoud Abd AL Kareem
  • 615
  • 2
  • 10
  • 23

1 Answers1

2

You can use the onChangeTab prop in the <Tabs> tag to listen the tab change event like the example below:

<Tabs 
  renderTabBar={()=> <ScrollableTab />} 
  onChangeTab={(obj) => {
    // obj = {
    //   i: currentPage,
    //   ref: this._children()[currentPage],
    //   from: prevPage,
    // }
    //do something with obj
  }}>
    <Tab heading="Tab1">
      <Tab1 />
    </Tab>
    <Tab heading="Tab2">
      <Tab2 />
    </Tab>
</Tabs>
Vahid Boreiri
  • 3,418
  • 1
  • 19
  • 34