英文:
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 "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 what the page where the TouchableOpacity navigate:
const ScanScreen = ({ navigation }) => {
return (
<View style={styles.screen}>
<Text>This is the Scan screen</Text>
</View>
);
};
i just wanted to create a button
do you know another way to create a round button?
答案1
得分: 0
<TouchableOpacity onPress={() => navigate('/details')}>
{/* Your button content */}
</TouchableOpacity>
英文:
<TouchableOpacity onPress={() => navigate('/details')}>
{/* 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 '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>
);
};
答案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
<Tab.Screen
name="main"
component={MainScreen}
options={{ headerShown: false }}
/>
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 }) => {}
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 }) => {
return (
<View style={styles.screen}>
<Text>This is the Scan screen</Text>
</View>
);
};
答案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 = () => {
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;
<!-- 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 }) => {
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>
);
};
<!-- 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) => 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>
)
}}
/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论