简体   繁体   English

React Navigation ReferenceError:找不到变量:isSelectionModeEnabled

[英]React Navigation ReferenceError: Can't find variable: isSelectionModeEnabled

I'm using React Navigation in a react-native app.我在 react-native 应用程序中使用 React Navigation。 So I followed the docs at https://reactnavigation.org/docs/custom-android-back-button-handling/ to implement Custom Android back button behavior using this snippet.因此,我按照https://reactnavigation.org/docs/custom-android-back-button-handling/上的文档使用此代码段实现自定义 Android 后退按钮行为。

Screen One.js屏幕一.js

import React, {useCallback} from 'react';
import {useFocusEffect} from '@react-navigation/native';
import {
 View,
 Text,
 BackHandler,
} from 'react-native';

export function ScreenOne({navigation}) {
 
 useFocusEffect(
   useCallback(() => {
     const onBackPress = () => {
       if (isSelectionModeEnabled()) {
         disableSelectionMode();
         return true;
       } else {
         return false;
       }
     };

     BackHandler.addEventListener('hardwareBackPress', onBackPress);

     return () =>
       BackHandler.removeEventListener('hardwareBackPress', onBackPress);
   }, [isSelectionModeEnabled, disableSelectionMode])
 );

 return (
   <View>
     <Text>Demo</Text>
   </View>
 )
}

Stack.js堆栈.js

import React from 'react';
import {createStackNavigator, HeaderBackButton} from '@react-navigation/stack';
import {
  ScreenOne,
  ScreenTwo,
} from '../screens';

const Stack = createStackNavigator();

function AppStack() {
  return (
    <Stack.Navigator>
      <Stack.Screen
        name="ScreenTwo"
        component={ScreenTwo}
        options={{headerShown: false}}
      />
      <Stack.Screen
        name="ScreenOne"
        component={ScreenOne}
        options={({navigation}) => ({
          headerShown: true,
          headerLeft: () => (
            <HeaderBackButton
              onPress={() => {
                navigation.replace('ScreenTwo');
              }}
            />
          ),
        })}
      />
    </Stack.Navigator>
  );
}

export default AppStack;

RootNavigation.js根导航.js

import {createRef} from 'react';

export const isReadyRef = createRef();
export const navigationRef = createRef();

export function navigate(name, params) {
  navigationRef.current?.navigate(name, params);
}

export function goBack() {
  navigationRef.current?.goBack();
}

App.js应用程序.js

import 'react-native-gesture-handler';
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import Stack from './navigators/Stack';
import {navigationRef} from './navigators/RootNavigation';
import {AuthContext} from './context';

const App = () => {
  return (
    <AuthContext.Provider>
      <NavigationContainer ref={navigationRef}>
        <Stack />
      </NavigationContainer>
    </AuthContext.Provider>
  );
};

export default App;

When I press the app back button, I keep getting this error:当我按下应用程序后退按钮时,我不断收到此错误:

ReferenceError: Can't find variable: isSelectionModeEnabled ReferenceError:找不到变量:isSelectionModeEnabled

isSelectionModeEnabled and disableSelectionMode is just an example. isSelectionModeEnableddisableSelectionMode只是一个示例。 Similar to "if Modal is open, close it instead of navigating back, otherwise keep the default back button behaviour".类似于“如果 Modal 打开,关闭它而不是向后导航,否则保持默认的后退按钮行为”。

You do not need it and you should instead implement your own logic for your own use case and return true to prevent the default navigation!您不需要它,您应该为自己的用例实现自己的逻辑并返回true以防止默认导航! Returning false will keep the default back button behaviour.返回false将保留默认的后退按钮行为。

If you just want to prevent the back button in any case, just do this:如果您只想在任何情况下阻止返回按钮,只需执行以下操作:

 const onBackPress = () => {
   return true;
 };

Here the relevant documentation: https://reactnative.dev/docs/backhandler这里的相关文档: https://reactnative.dev/docs/backhandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM