最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

React native headerLeft onPress not working - Stack Overflow

programmeradmin0浏览0评论

I am using @react-navigation/native and @react-navigation/native-stack

I have the following code

    useEffect(() => {
      navigation.setOptions({
        headerLeft: (props) => <HeaderBackButton {...props} tintColor={gold} onPress={() => navigation.navigate('back)} />
      });
    }, [navigation]);

But clicking on the back button I can see the opacity effect but nothing more, no navigation, I updated my code to do more debug

    useEffect(() => {
      console.log('loaded')
      navigation.setOptions({
        headerLeft: (props) => <TouchableOpacity onPress={() => console.log('test')} ><Text style={{fontSize: 40}} >{"< Back"}</Text></TouchableOpacity>
      });
    }, []);

I was thinking the problem was related to HeaderBackButton but no, with a TouchableOpacity I have the same issue In this second code the loaded is logged but clicking on the TouchableOpacity I can see the opacity effect but the test is never logged

I am using @react-navigation/native and @react-navigation/native-stack

I have the following code

    useEffect(() => {
      navigation.setOptions({
        headerLeft: (props) => <HeaderBackButton {...props} tintColor={gold} onPress={() => navigation.navigate('back)} />
      });
    }, [navigation]);

But clicking on the back button I can see the opacity effect but nothing more, no navigation, I updated my code to do more debug

    useEffect(() => {
      console.log('loaded')
      navigation.setOptions({
        headerLeft: (props) => <TouchableOpacity onPress={() => console.log('test')} ><Text style={{fontSize: 40}} >{"< Back"}</Text></TouchableOpacity>
      });
    }, []);

I was thinking the problem was related to HeaderBackButton but no, with a TouchableOpacity I have the same issue In this second code the loaded is logged but clicking on the TouchableOpacity I can see the opacity effect but the test is never logged

Share Improve this question asked Jan 31 at 18:06 AjouveAjouve 10k27 gold badges94 silver badges153 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If you're inside a screen component, you can access the navigation object via props or the useNavigation hook and call goBack() to return to the previous screen.

import React from 'react';
import { Button, View } from 'react-native';
import { useNavigation } from '@react-navigation/native';

const MyScreen = () => {
  const navigation = useNavigation();

  return (
    <View>
      <Button
        title="Go Back"
        onPress={() => navigation.goBack()} // Navigates back to the previous screen
      />
    </View>
  );
};

export default MyScreen;

Use onPressOut instead of onPress This issue is caused by RN Screens. Check out these for more details:

  • react-native-screens issue
  • Expo issue
发布评论

评论列表(0)

  1. 暂无评论