在嵌套屏幕中调用 navigation.isFocused() 总是返回 false

时间:2021-03-02 18:28:28

标签: react-navigation

我有一个嵌套在标签导航器中的堆栈导航器,它是这样的:

const CategoryStack = createStackNavigator();

function CategoryStackScreen() {
  return (
    <CategoryStack.Navigator>
      <CategoryStack.Screen name='Category' component={CategoryScreen} />
      <CategoryStack.Screen name='ProductDetail' component={ProductDetailScreen} />
    </CategoryStack.Navigator>
  );
}

const BottomTab = createBottomTabNavigator();

class AppNavigation extends React.Component {
  render() {
    return (
      <NavigationContainer>
        <BottomTab.Navigator>
          <BottomTab.Screen name='Home' component={HomeScreen} />
          <BottomTab.Screen name='Category' component={CategoryStackScreen} />
        </BottomTab.Navigator>
      </NavigationContainer>
    );
  }
}

在 CategoryScreen 内,我添加了一个 tabPress 侦听器:

class CategoryScreen extends React.Component {
  componentDidMount() {
    let { navigation } = this.props;
    this._unsubscribe = navigation.dangerouslyGetParent().addListener('tabPress', (e) => {
      console.log(navigation.isFocused());
    });
  }

  componentWillUnmount() {
    this._unsubscribe();
  }
}

但是当我从一个选项卡切换到另一个选项卡时,navigation.isFocused() 总是返回 false。

有人可以帮忙吗?

0 个答案:

没有答案