reactjs 我想使用TouchableOpacity导航到另一个页面,但失败

rqmkfv5c  于 2023-05-28  发布在  React
关注(0)|答案(6)|浏览(109)

我想使用TouchableOpacity导航到另一个页面,但失败使用Tab导航到其他页面。导航器工作正常。我在那之后创建了TouchableOpacity。它位于其他按钮的中间,它应该导航到扫描仪页面这里是我的代码:

import React from "react";
import { StyleSheet, TouchableOpacity } from "react-native";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { NavigationContainer } from "@react-navigation/native";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import MainScreen from "./view/MainPage";
import ProfileScreen from "./view/ProfilePage";
import ScanScreen from "./view/ScanPage";

const Tab = createBottomTabNavigator();

const App = ({ navigation }) => {
  return (
    <>
      <NavigationContainer>
        <Tab.Navigator
          style={styles.tabBar}
          screenOptions={({ route }) => ({
            tabBarIcon: ({ focused, color, size }) => {
              let iconName;

              if (route.name === "main") {
                iconName = focused ? "home" : "home";
              } else if (route.name === "scan") {
                iconName = focused ? "qrcode-scan" : "qrcode-scan";
              } else if (route.name === "profile") {
                iconName = focused ? "account" : "account";
              }

              return (
                <MaterialCommunityIcons
                  name={iconName}
                  size={size}
                  color={color}
                />
              );
            },
            tabBarLabel: "",
            tabBarActiveTintColor: "#1573FE",
            tabBarInactiveTintColor: "gray",
          })}
        >
          <Tab.Screen
            name="main"
            component={MainScreen}
            options={{ headerShown: false }}
          />
         
          <Tab.Screen
            name="profile"
            component={ProfileScreen}
            options={{ headerShown: false }}
          />
        </Tab.Navigator>

        <TouchableOpacity
          style={styles.scanButton}
          onPress={() => navigation.navigate("ScanScreen")}
        >
          <MaterialCommunityIcons name="qrcode-scan" color="white" size={30} />
        </TouchableOpacity>
      </NavigationContainer>
    </>
  );
};

这是TouchableOpacity导航的页面:

const ScanScreen = ({ navigation }) => {
  return (
    <View style={styles.screen}>
      <Text>This is the Scan screen</Text>
    </View>
  );
};

我只是想创建一个按钮

你知道另一种制作圆形按钮的方法吗?

pdkcd3nj

pdkcd3nj1#

<TouchableOpacity onPress={() => navigate('/details')}>

{/* 您的按钮内容 */}

d6kp6zgx

d6kp6zgx2#

创建自定义组件。因为navigation prop不存在。您只能在NavigationContainer组件下获得navigation

import React from 'react';
import { TouchableOpacity } from 'react-native';

import { MaterialCommunityIcons } from '@expo/vector-icons';
import { useNavigation } from '@react-navigation/native';

export const Button = () => {
  const navigate = useNavigation();

  return (
    <TouchableOpacity
      style={styles.scanButton}
      onPress={() => navigate('ScanScreen')}>
      <MaterialCommunityIcons name="qrcode-scan" color="white" size={30} />
    </TouchableOpacity>
  );
};
pgpifvop

pgpifvop3#

navigation prop被传递给screen组件。您可以在导航器中定义屏幕组件。例如,在代码中,这是

<Tab.Screen
        name="main"
        component={MainScreen}
        options={{ headerShown: false }}
      />

一个屏幕组件和MainScreen将接收navigation,您可以导航到另一个屏幕组件。您的应用组件未接收navigation prop

// this is just a custom component, not a screen component
 const App = ({ navigation }) => {}

此外,如果您没有将ScanScreen注册为导航器中的屏幕组件,它将不会收到navigation prop

// you have to register this as a screen component
const ScanScreen = ({ navigation }) => {
  return (
    <View style={styles.screen}>
      <Text>This is the Scan screen</Text>
    </View>
  );
};
hi3rlvi2

hi3rlvi24#

检查组件的名称是否拼写正确,即ScanScreen与路由文件中的名称相同。其次检查它们是否在同一堆栈中。

gab6jxml

gab6jxml5#

应用程序组件不直接与导航堆栈相关联,它不能直接访问导航属性。要解决这个问题,您可以使用NavigationContainer Package App组件,并将TouchableOpacity移动到MainScreen组件中。

const App = () => {
  return (
    <NavigationContainer>
      <Tab.Navigator
        style={styles.tabBar}
        screenOptions={screenOptions}
      >
        <Tab.Screen
          name="main"
          component={MainScreen}
          options={{ headerShown: false }}
        />
        <Tab.Screen
          name="scan"
          component={ScanScreen}
          options={{ headerShown: false }}
        />
        <Tab.Screen
          name="profile"
          component={ProfileScreen}
          options={{ headerShown: false }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

export default App;

MainScreen组件,您可以添加TouchableOpacity进行扫描:

const MainScreen = ({ navigation }) => {
  return (
    <View style={styles.container}>
      <Text>Main Screen</Text>
      <TouchableOpacity
        style={styles.scanButton}
        onPress={() => navigation.navigate("scan")}
      >
        <MaterialCommunityIcons name="qrcode-scan" color="white" size={30} />
      </TouchableOpacity>
    </View>
  );
};
gfttwv5a

gfttwv5a6#

你需要传递一个自定义的Button/Icon组件到选项中的tabBarButton属性。我就是这么处理的。

export const CustomTabBarButton = ({
  children,
  onPress,
}: {
  children: React.ReactNode;
  onPress: (e) => void;
}) => (
  <TouchableOpacity
    style={{
      top: -1,
      right: 9,
    }}
    onPress={onPress}
  >
    <View
      style={{
        width: 80,
        height: 80,
        borderRadius: 35,
        justifyContent: "center",
      }}
    >
      {children}
    </View>
  </TouchableOpacity>
);



  <Tab.Screen
    name={SCREEN_CONFIG.SEARCH}
    component={Search}
    options={{
      headerShown: false,
      title: SCREEN_CONFIG.SEARCH,
      tabBarLabelPosition: "below-icon",
      tabBarButton: props => (
        <CustomTabBarButton
          onPress={e => {
            props.onPress?.(e);
          }}
        >
          <View
            style={{
              backgroundColor: COLORS[theme].TAB_BAR_BG,
              borderRadius: 50,
              width: 100,
              height: 100,
              justifyContent: "center",
              alignItems: "center"
            }}
          >
            <PlusIcon />
          </View>
        </CustomTabBarButton>
      )
    }}
  />

相关问题