React-native-navigation: Bottom tabs: Incorrect componentDidAppear lifecycle method being executed

Created on 4 Jan 2021  路  3Comments  路  Source: wix/react-native-navigation

馃悰 Bug Report

When using bottom tabs, if selecting a different default tab other than the first one, its componentDidAppear lifecycle method is not executed. Instead, the first tab's componentDidAppear method is executed.

Have you read the Contributing Guidelines on issues?

Yes

To Reproduce

  1. Install RNN according to the guides. npm i react-native-navigation and npx rnn-link.
  2. Use the following index.js and Screen.js files.
    index.js
import {Navigation} from 'react-native-navigation';
import Screen from './Screen';

Navigation.registerComponent('Screen', () => Screen);

Navigation.events().registerAppLaunchedListener(() => {
  Navigation.setDefaultOptions({
    bottomTab: {
      selectedTextColor: 'red',
    },
  });

  Navigation.setRoot({
    root: {
      bottomTabs: {
        id: 'BottomTabs',
        options: {
          bottomTabs: {
            currentTabId: 'Screen2',
          },
        },
        children: [
          {
            component: {
              id: 'Screen1',
              name: 'Screen',
              options: {
                bottomTab: {
                  text: 'Screen 1',
                },
              },
            },
          },
          {
            component: {
              id: 'Screen2',
              name: 'Screen',
              options: {
                bottomTab: {
                  text: 'Screen 2',
                },
              },
            },
          },
        ],
      },
    },
  });
});

Screen.js

import React, {useEffect} from 'react';
import {Navigation} from 'react-native-navigation';
import {SafeAreaView, StyleSheet, Text} from 'react-native';

const Screen = ({componentId}) => {
  useEffect(() => {
    const unsubscribe = Navigation.events().registerComponentListener(
      {
        componentDidAppear: () =>
          console.log(`componentDidAppear ${componentId}`),
        componentDidDisappear: () =>
          console.log(`componentDidDisappear ${componentId}`),
      },
      componentId,
    );
    return () => unsubscribe.remove();
  }, [componentId]);
  return (
    <SafeAreaView style={styles.root}>
      <Text>Screen {componentId}</Text>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  root: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'green',
  },
});

export default Screen;

  1. Run the app with npx react-native run-android.
  2. Observe the logs

Expected behavior

componentDidAppear Screen2 should have appeared in the logs

Actual Behavior

componentDidAppear Screen1 appeared in the logs instead.

Your Environment

  • React Native Navigation version: 7.7.0
  • React Native version: 0.63.4
  • Platform(s) (iOS, Android, or both?): Only Android

Reproducible Demo

https://github.com/HectorRicardo/bugrnn

Android acceptebug

Most helpful comment

@yogevbd You're right, my bad, sorry about that. I updated the bug description to indicate that it only happens on Android.

All 3 comments

I've seen this on my app as well, have had to engineer a workaround.

@HectorRicardo Are you sure it reproduce also on iOS? I couldn't reproduce it in your demo

@yogevbd You're right, my bad, sorry about that. I updated the bug description to indicate that it only happens on Android.

Was this page helpful?
0 / 5 - 0 ratings