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

huangapple go评论56阅读模式
英文:

I want to navigate to another page using TouchableOpacity but failed

问题

Here is the translated code portion:

我想使用TouchableOpacity导航到另一个页面但失败了
使用Tab.Navigator导航到其他页面正常工作我在那之后创建了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>
    </>
  );
};

This is the translated code portion from your request. If you have any further questions or need assistance with other parts, please feel free to ask.

英文:

I want to navigate to another page using TouchableOpacity but failed
Navigating to other pages with Tab.Navigator works correctly. I created after that TouchableOpacity. Which is located in the middle of the other buttons and it should navigate to the scanner page
Here is my code:

import React from &quot;react&quot;;
import { StyleSheet, TouchableOpacity } from &quot;react-native&quot;;
import { createBottomTabNavigator } from &quot;@react-navigation/bottom-tabs&quot;;
import { NavigationContainer } from &quot;@react-navigation/native&quot;;
import { MaterialCommunityIcons } from &quot;@expo/vector-icons&quot;;
import MainScreen from &quot;./view/MainPage&quot;;
import ProfileScreen from &quot;./view/ProfilePage&quot;;
import ScanScreen from &quot;./view/ScanPage&quot;;
const Tab = createBottomTabNavigator();
const App = ({ navigation }) =&gt; {
return (
&lt;&gt;
&lt;NavigationContainer&gt;
&lt;Tab.Navigator
style={styles.tabBar}
screenOptions={({ route }) =&gt; ({
tabBarIcon: ({ focused, color, size }) =&gt; {
let iconName;
if (route.name === &quot;main&quot;) {
iconName = focused ? &quot;home&quot; : &quot;home&quot;;
} else if (route.name === &quot;scan&quot;) {
iconName = focused ? &quot;qrcode-scan&quot; : &quot;qrcode-scan&quot;;
} else if (route.name === &quot;profile&quot;) {
iconName = focused ? &quot;account&quot; : &quot;account&quot;;
}
return (
&lt;MaterialCommunityIcons
name={iconName}
size={size}
color={color}
/&gt;
);
},
tabBarLabel: &quot;&quot;,
tabBarActiveTintColor: &quot;#1573FE&quot;,
tabBarInactiveTintColor: &quot;gray&quot;,
})}
&gt;
&lt;Tab.Screen
name=&quot;main&quot;
component={MainScreen}
options={{ headerShown: false }}
/&gt;
&lt;Tab.Screen
name=&quot;profile&quot;
component={ProfileScreen}
options={{ headerShown: false }}
/&gt;
&lt;/Tab.Navigator&gt;
&lt;TouchableOpacity
style={styles.scanButton}
onPress={() =&gt; navigation.navigate(&quot;ScanScreen&quot;)}
&gt;
&lt;MaterialCommunityIcons name=&quot;qrcode-scan&quot; color=&quot;white&quot; size={30} /&gt;
&lt;/TouchableOpacity&gt;
&lt;/NavigationContainer&gt;
&lt;/&gt;
);
};

This is what the page where the TouchableOpacity navigate:

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

i just wanted to create a button
我想使用 TouchableOpacity 导航到另一个页面,但失败了。

do you know another way to create a round button?

答案1

得分: 0

<TouchableOpacity onPress={() => navigate('/details')}>
  {/* Your button content */}
</TouchableOpacity>
英文:
&lt;TouchableOpacity onPress={() =&gt; navigate(&#39;/details&#39;)}&gt;

{/* Your button content */}
</TouchableOpacity>

答案2

得分: 0

Create the custom component. Because navigation prop doesn't exist. You can get navigation only under NavigationContainer component.

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>
  );
};
英文:

Create the custom component. Because navigation prop doesn't exist. You can get navigation only under NavigationContainer component.

import React from &#39;react&#39;;
import { TouchableOpacity } from &#39;react-native&#39;;

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

export const Button = () =&gt; {
  const navigate = useNavigation();

  return (
    &lt;TouchableOpacity
      style={styles.scanButton}
      onPress={() =&gt; navigate(&#39;ScanScreen&#39;)}&gt;
      &lt;MaterialCommunityIcons name=&quot;qrcode-scan&quot; color=&quot;white&quot; size={30} /&gt;
    &lt;/TouchableOpacity&gt;
  );
};

答案3

得分: 0

navigation属性传递给screen组件。您在导航器内定义屏幕组件。例如,在您的代码中,这是

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

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

// 这只是一个自定义组件,而不是屏幕组件
const App = ({ navigation }) => {}

此外,如果您没有将ScanScreen注册为导航器内的屏幕组件,则它将不会接收navigation属性

// 您必须将此注册为屏幕组件
const ScanScreen = ({ navigation }) => {
  return (
    <View style={styles.screen}>
      <Text>This is the Scan screen</Text>
    </View>
  );
};
英文:

navigation prop is passed to the screen components. you define the screen components inside the navigators. For example in your code, this is

    &lt;Tab.Screen
name=&quot;main&quot;
component={MainScreen}
options={{ headerShown: false }}
/&gt;

a screen component and MainScreen will receive navigation and you can navigate to another screen component. your app component does not receive navigation prop

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

Also, if you did not register the ScanScreen as a screen component inside a navigator, it will not receive navigation prop

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

答案4

得分: 0

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

英文:

Check whether the name of the component is correctly spelled i.e ScanScreen is the same as in your routes file .Secondly check whether they both are in same stack .

答案5

得分: 0

App组件与导航堆栈没有直接关联,因此它无法直接访问导航属性。要解决这个问题,您可以使用NavigationContainer包装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>
  );
};
英文:

App component is not directly associated with the navigation stack, it doesn't have direct access to the navigation prop. To resolve this, you can wrap the App component with the NavigationContainer and move the TouchableOpacity inside the MainScreen component.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const App = () =&gt; {
return (
&lt;NavigationContainer&gt;
&lt;Tab.Navigator
style={styles.tabBar}
screenOptions={screenOptions}
&gt;
&lt;Tab.Screen
name=&quot;main&quot;
component={MainScreen}
options={{ headerShown: false }}
/&gt;
&lt;Tab.Screen
name=&quot;scan&quot;
component={ScanScreen}
options={{ headerShown: false }}
/&gt;
&lt;Tab.Screen
name=&quot;profile&quot;
component={ProfileScreen}
options={{ headerShown: false }}
/&gt;
&lt;/Tab.Navigator&gt;
&lt;/NavigationContainer&gt;
);
};
export default App;

<!-- end snippet -->

MainScreen component, you can add the TouchableOpacity for scanning:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

答案6

得分: 0

以下是代码部分的中文翻译:

你需要将一个自定义的Button/Icon组件传递给options中的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>
    )
  }}
/>

请注意,这是代码部分的翻译。如果您有其他问题或需要进一步帮助,请随时提出。

英文:

You need to pass a custom Button/Icon component to tabBarButton prop in options. That's how I handle it.

export const CustomTabBarButton = ({
children,
onPress,
}: {
children: React.ReactNode;
onPress: (e) =&gt; void;
}) =&gt; (
&lt;TouchableOpacity
style={{
top: -1,
right: 9,
}}
onPress={onPress}
&gt;
&lt;View
style={{
width: 80,
height: 80,
borderRadius: 35,
justifyContent: &quot;center&quot;,
}}
&gt;
{children}
&lt;/View&gt;
&lt;/TouchableOpacity&gt;
);
&lt;Tab.Screen
name={SCREEN_CONFIG.SEARCH}
component={Search}
options={{
headerShown: false,
title: SCREEN_CONFIG.SEARCH,
tabBarLabelPosition: &quot;below-icon&quot;,
tabBarButton: props =&gt; (
&lt;CustomTabBarButton
onPress={e =&gt; {
props.onPress?.(e);
}}
&gt;
&lt;View
style={{
backgroundColor: COLORS[theme].TAB_BAR_BG,
borderRadius: 50,
width: 100,
height: 100,
justifyContent: &quot;center&quot;,
alignItems: &quot;center&quot;
}}
&gt;
&lt;PlusIcon /&gt;
&lt;/View&gt;
&lt;/CustomTabBarButton&gt;
)
}}
/&gt;

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

huangapple
  • 本文由 发表于 2023年5月25日 19:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定