简体   繁体   English

React Native Navigation 覆盖 Stack Navigator 中 header 后退按钮的 goBack()

[英]React Native Navigation override goBack() of header back button in Stack Navigator

I want to customize the behavior of the default back button in stack navigator locally to one screen.我想在本地自定义堆栈导航器中默认后退按钮的行为到一个屏幕。

In the details assuming that on the stack there are screen1|screen2, I want to pass some props from screen2 to screen1 once the button is pressed.在假设堆栈上有screen1 | screen2的细节中,一旦按下按钮,我想将一些道具从screen2传递到screen1。

I spent a lot of time reading React navigation docs, searching on internet and coding but I am not able to do this.我花了很多时间阅读 React 导航文档,在互联网上搜索和编码,但我无法做到这一点。

FROM DOCS来自文档

It's possible that in some circumstances that you want to customize the back button more than you can through the options mentioned above, in which case you can set the headerLeft option to a React Element that will be rendered I know that the issue concerns the goBack() function of the headerRight component.在某些情况下,您可能希望通过上述选项自定义后退按钮,在这种情况下,您可以将 headerLeft 选项设置为将呈现的 React Element 我知道问题与 goBack( ) headerRight 组件的 function。

I want to override the default function goBack() related to the headerLeft back button with something like navigation.navigate("previousScreen",{{..props}}).我想用类似 navigation.navigate("previousScreen",{{..props}}) 的方式覆盖与 headerLeft 后退按钮相关的默认 function goBack()。

And ( this is very important,. ) I want to use this behavior locally to a specific screen, so not globally.而且(这非常重要,。)我想在本地使用此行为到特定屏幕,而不是全局。

I have tried with something like this but doesn't works.我已经尝试过这样的事情,但不起作用。

 export default function App(){
 return(
 <NavigationContainer>
  <Stack.Navigator>
    <Stack.Screen name="FirstScreen" component={FirstScreen}/>
    <Stack.Screen name="SecondScreen" component={SecondScreen} options={{headerLeft: () => (
        <HeaderBackButton
          onPress={() =>navigation.navigate("FirstScreen",{//stuff//})}
          title="Info"
          color="#fff"
        />
      ),}}/>
  </Stack.Navigator>
</NavigationContainer>
 )}

If you are using react navigation v5, you can set the behaviour for a specific screen using:如果您使用的是反应导航 v5,您可以使用以下命令设置特定屏幕的行为:

import { HeaderBackButton } from '@react-navigation/stack';

...
options={{
          headerLeft: (props) => (
            <HeaderBackButton
              {...props}
              onPress={() => {
                navigation.navigate('screenName');
              }}
            />
          ),
        }}
...

You can also set as stack level using screenOptions={{}} instead.您也可以改为使用 screenOptions={{}} 设置为堆栈级别。

"navigation" and "route" are also available on screen props. “导航”和“路线”也可以在屏幕道具上使用。

options={({ navigation, route }) => ({headerLeft: ...})

When the component is mounted, register back button press listener安装组件后,注册后退按钮按下监听器

componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
}

componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
}

Then in the function, you can check and handle the action, ex:然后在function中,可以查看和处理action,例如:

onBackButtonPressAndroid = () => {
    // Check if the current screen is focused or a subscreen is perhaps
    if (this.props.navigation.isFocused()) {
        if (someCondition) {
            // Do a navigation to a custom screen, pass props here if needed
            this.props.navigation.navigate('search')
            return true
            // Return true if you want to tell react navigation you've handled the back press
        }
        else if (this.state.offsetTop > 10) {
            // Ex. for scrolling to top
            this.scrollToTop()
            this.setState({
                offsetTop: 0,
            })
            return true
        }
    }
    // Return false if you want to tell react navigation you didn't handle the back press
    return false
}

Try passing navigationOptions to a specific screen:尝试将navigationOptions传递到特定屏幕:

export default function App(){
 return(
 <NavigationContainer>
  <Stack.Navigator>
    <Stack.Screen name="FirstScreen" component={FirstScreen}/>
    <Stack.Screen name="SecondScreen" component={SecondScreen}
       navigationOptions: ({ navigation }) => ({ headerLeft: (<HeaderBackButton onPress={() => {}}/>)
    })}}/>
  </Stack.Navigator>
</NavigationContainer>
 )}

Either you can specify it there on the navigation .您可以在navigation中指定它。 Or on the Second screen , try this:或者在Second screen ,试试这个:

class SecondScreen extends React.Component {
  static navigationOptions = {
    headerLeft: (
      <Button
        onPress={() => alert('This is a back button!')}
        title="Title"
        color="#fff"
      />
    ),
  };
}

For react-navigation v6, you can user setOptions inside a useEffect对于 react-navigation v6,您可以在 useEffect 中使用 setOptions

import { HeaderBackButton } from '@react-navigation/elements';

...

const ChatScreen = ({navigation}) => {

     useEffect( () => {
            navigation.setOptions({ headerShown: true,
                                    headerLeft: (props) => (
                                        <HeaderBackButton
                                            {...props}
                                            onPress={() => {
                                                navigation.navigate('MyScreen');
                                            }}
                                        />
                                    )
                                  });
        } ); 
}

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

相关问题 带有标题后退按钮的 React 导航抽屉导航器 - React navigation drawer navigator with header back button 反应本机堆栈导航goBack()不起作用 - React native stack navigation goBack() not working 在 React-Native 中,尝试访问从 header 后退按钮中的堆栈导航器打开的抽屉 - In React-Native, trying to access the drawer open from stack navigator in header back button React Native中带抽屉和堆栈导航器的嵌套导航 - Nested Navigation with drawer and stack navigator in React Native 使用Stack Navigator进行React-Native导航 - React-Native navigation with Stack Navigator 反应本机Stack导航器Back Handler - React native Stack navigator Back Handler 在 React Native 和 React Navigation 中从 header 中删除后退按钮 - Remove back button from header in React Native and React Navigation React Native 在抽屉导航屏幕上显示标题和后退按钮 - React Native show header and back button on drawer navigation screen 无法使用反应原生导航、堆栈导航器渲染导航屏幕 - Unable to render navigation screen using react native navigation, stack navigator 如何在本机反应中更改堆栈导航器中的后退按钮图像? - How do I change the back button image in stack navigator in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM