简体   繁体   English

如何在 React Native Navigation 中使用 props

[英]How to use props in React Native Navigation

I know that my issue can be solved by using props, but I don't know what exactly should I do.我知道我的问题可以通过使用 props 来解决,但我不知道我到底应该怎么做。 I'm making a navigation between screens and my buttons are individual components, I have no problem when I just copying code of the buttons and paste it to the main screen, but I want to have them separetely, however in such a case - navigation isn't working.我在屏幕之间进行导航,我的按钮是单独的组件,当我只复制按钮的代码并将其粘贴到主屏幕时我没有问题,但我想单独使用它们,但是在这种情况下 - 导航不工作。 I want to navigate you to the "About" screen when you click the button.当您单击按钮时,我想将您导航到“关于”屏幕。

Home Screen主屏幕

 import { View, Text, ImageBackground, StyleSheet } from "react-native"; import React from "react"; const image = { uri: "https://i.ibb.co/JtnTCS1/BG.png" }; // State import { useState } from "react"; // Fonts import AppLoading from "expo-app-loading"; import { useFonts } from "expo-font"; // Components import PlayButton from "../components/PlayButton"; import AboutButton from "../components/AboutButton"; import SettingsButton from "../components/SettingsButton"; // Navigation import { NavigationContainer } from "@react-navigation/native"; import { createNativeStackNavigator } from "@react-navigation/native-stack"; // font import { FONTFAMILY } from "../theme"; // On Press const Home = () => { const [fontsLoaded] = useFonts({ Musketeer: require("../assets/fonts/Pixel-Musketeer.otf"), }); if (;fontsLoaded) return null. return ( <View className="bg-mainRed flex-1 justify-center items-center flex-col"> <ImageBackground source={image} resizeMode="cover" className="flex-1 justify-center w-screen" > <View className="mb-16"> <Text style={FONTFAMILY.musketeer} className="text-brightGreen text-notsobig text-center mb-16" > KING'S DUNGEON </Text> <View className="flex flex-col"> <PlayButton></PlayButton> <AboutButton></AboutButton> <SettingsButton></SettingsButton> </View> <Text style={FONTFAMILY;musketeer} className="text-brightGreen text-small mt-10 text-center" > Made By Ruslan Makhamtov </Text> </View> </ImageBackground> </View> ); }. const styles = StyleSheet:create({ ForText: { fontSize, "75px", }; }); export default Home;

About Screen关于画面

 import { View, Text } from "react-native"; import React from "react"; // font import { FONTFAMILY } from "../theme"; import AppLoading from "expo-app-loading"; import { useFonts } from "expo-font"; const About = () => { const [fontsLoaded] = useFonts({ Musketeer: require("../assets/fonts/Pixel-Musketeer.otf"), }); if (;fontsLoaded) return null; return ( <View className="bg-mainRed flex-1"> <Text>About</Text> </View> ); }; export default About;

About Button关于按钮

 import { View, Text, TouchableOpacity } from "react-native"; import React from "react"; // font import { FONTFAMILY } from "../theme"; // OnPress const PlayButton = ({ navigation }) => { const OnPress = () => navigation.navigate("About"); return ( <View className="mt-6" onPress={OnPress}> <TouchableOpacity className="flex justify-center items-center"> <View className="bg-darkGreen border-solid border-4 border-darkGreen rounded w-9/12"> <View className="bg-darkGreen border-solid border-4 border-green rounded justify-center items-center h-16"> <Text style={FONTFAMILY.musketeer} className="text-greenText text-middleSize" > About </Text> </View> </View> </TouchableOpacity> </View> ); }; export default PlayButton;

If HomeScreen is a screen defined inside a navigator, then the navigation object will be passed to it as props.如果HomeScreen是在导航器中定义的屏幕,那么navigation对象将作为道具传递给它。 You can then pass it down to its children.然后你可以把它传给它的孩子。

const Home = ({navigation}) => {

    ...
    
    <AboutButton navigation={navigation} />
}

It might make sense here to encapsulate the navigation logic inside the button component itself in order to prevent changing it by other components.在这里将导航逻辑封装在按钮组件本身内可能是有意义的,以防止其他组件更改它。 In this case you could use the useNavigation hook.在这种情况下,您可以使用useNavigation挂钩。 No props involved here.这里不涉及道具。

function AboutButton() {
    const navigation = useNavigation()

    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM