首页 > 解决方案 > 反应导航幻灯片动画后退按钮错误

问题描述

我收到此错误:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
    in Card (at CardContainer.tsx:206)
    in CardContainer (at CardStack.tsx:623)
    in RNSScreen (at createAnimatedComponent.js:217)
    in AnimatedComponent (at createAnimatedComponent.js:278)
    in AnimatedComponentWrapper (at src/index.native.tsx:147)
    in Screen (at Screens.tsx:74)
    in MaybeScreen (at CardStack.tsx:616)

这是我的导航器:

const DefaultStackNavigation = () => {
  return(
    <NavigationContainer fallback={<View style={{backgroundColor: "#0C0C0C", height: "100%", width: "100%", flex: 1}} />}>
      <Stack.Navigator screenOptions={{headerShown: false, gestureEnabled: true, gestureDirection: "horizontal", cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS, cardStyle: {backgroundColor: "transparent", flex: 1, height: "100%", width: "100%"}}} independent={false}>
        <Stack.Screen name="HomeTabs" component={HomeScreen} />
        <Stack.Screen name="ChatScreen" component={ChatScreen} />
        <Stack.Screen name="Settings" component={SettingsScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

因为我有gestureEnabled=true我可以向右滑动屏幕返回。但是现在有一个问题:

我希望用户选择他/她想要滑动返回还是单击返回按钮。如果我向右滑动屏幕,我不会收到错误消息,我可以使用该应用程序。但是,如果我单击navigation.goBack()ChatScreen 中的按钮,我会收到错误消息(本文开头)。如果我启用标题并单击 goBack 按钮,我也不会收到此错误。如何防止 React Native 以这种方式运行?

编辑

这是我的后退按钮:

<View style={styles.backArrowContainer}>
  <TouchableOpacity style={{height: 60, width: 60, justifyContent: "center", alignItems: "center"}} onPress={() => navigation.goBack()}>
    <BackArrow />
  </TouchableOpacity>
</View>

标签: javascriptreactjsreact-nativereact-navigation

解决方案


推荐阅读