react native - Expo, Why can't I use router in _layout? - Stack Overflow

admin2025-04-16  7

I try to implement a settings button in my header from the _layout, using an icon I made an button to redirect to "/settings", however it only works in browsers, when I try it in android, nothing happens.

import { Redirect, Stack, useRouter } from "expo-router";
import { TouchableOpacity } from "react-native";
import { Ionicons } from "@expo/vector-icons";

export default function RootLayout() {
  const router = useRouter();

  // Define the redirect function inside the component
  const redirect = () => {
    console.log("Navigating to settings");
    router.navigate("/settings");
  };
  
  return  (
    <Stack 
      screenOptions={{
        headerStyle: {
          backgroundColor: 'lightblue',
        },
        headerTitleStyle: {
          fontWeight: 'bold',
          color: 'white',
        },
        title: 'Weerklank',
        headerRight: () => (
          <TouchableOpacity
            onPress={redirect}
            style={{ marginRight: 15 }}
          >
            <Ionicons name="settings" size={24} color="white" />
          </TouchableOpacity>
        )
      }}>
      <Stack.Screen name="settings" 
        options={{
          title: 'Settings', // Override the title for the settings screen
      }}
    />
    </Stack>
  );
}

I try to implement a settings button in my header from the _layout, using an icon I made an button to redirect to "/settings", however it only works in browsers, when I try it in android, nothing happens.

import { Redirect, Stack, useRouter } from "expo-router";
import { TouchableOpacity } from "react-native";
import { Ionicons } from "@expo/vector-icons";

export default function RootLayout() {
  const router = useRouter();

  // Define the redirect function inside the component
  const redirect = () => {
    console.log("Navigating to settings");
    router.navigate("/settings");
  };
  
  return  (
    <Stack 
      screenOptions={{
        headerStyle: {
          backgroundColor: 'lightblue',
        },
        headerTitleStyle: {
          fontWeight: 'bold',
          color: 'white',
        },
        title: 'Weerklank',
        headerRight: () => (
          <TouchableOpacity
            onPress={redirect}
            style={{ marginRight: 15 }}
          >
            <Ionicons name="settings" size={24} color="white" />
          </TouchableOpacity>
        )
      }}>
      <Stack.Screen name="settings" 
        options={{
          title: 'Settings', // Override the title for the settings screen
      }}
    />
    </Stack>
  );
}
Share Improve this question edited Feb 4 at 1:46 Drew Reese 204k18 gold badges245 silver badges273 bronze badges asked Feb 4 at 0:08 kersjan14kersjan14 12 bronze badges 1
  • If router.navigate is not working on Android, try using router.push instead. It behaves similarly but might work better on Android: – nazmul Commented Feb 4 at 4:58
Add a comment  | 

1 Answer 1

Reset to default 0

Found the solution, TouchableOpacity doens't behave well on android when using OnPress, so the solution is to use OnPressIn

headerRight: () => (
  <TouchableOpacity
    onPressIn={redirect}
    style={{ marginRight: 15 }}
  >
    <Ionicons name="settings" size={24} color="white" />
  </TouchableOpacity>
)

This did the trick and now it works.

转载请注明原文地址:http://www.anycun.com/QandA/1744744410a87001.html