【问题标题】:Navigation, not working from Pulled functions导航,不适用于拉取功能
【发布时间】:2021-11-14 01:43:50
【问题描述】:

我会尽力解释这一点,我还是新手,很难理解术语。

我正在开发一个 react native 项目。我创建了两个文件。一个 Header.js 和一个 footer.js。

我已成功将它们拉入我的主屏幕。然而,footer.js 中的按钮不再触发。我明白了; undefined 不是错误的对象(评估“navigation.navigate”)。

如何恢复该功能?

非常感谢任何帮助。这是我的帮助项目; 应用.js

import React from 'react';
import TabNavigator from "./assets/component/TabNavigator";
import {StackNavigation} from "./assets/component/StackNavigation";


export default function App() {
    return (
       <StackNavigation/>
    );
}

StackNavigation.js;

import React from 'react';
import { HomeScreen} from "../Screens/HomeScreen";
import LayoutProps from "../Screens/Layout Props";
import SampleViewProps from "../Screens/SampleViewProps";
import {NavigationContainer} from "@react-navigation/native";
import {createNativeStackNavigator} from "@react-navigation/native-stack";
// import ShareExample from "./assets/component/SBShare";

const Stack = createNativeStackNavigator();

export function StackNavigation() {
    return (
        <NavigationContainer>
            <Stack.Navigator initialRouteName="Home">
                <Stack.Screen name="Home" component={HomeScreen}/>
                <Stack.Screen name="Layout Props" component={LayoutProps} />
                <Stack.Screen name="View Props" component={SampleViewProps} />
            </Stack.Navigator>
        </NavigationContainer>
    );
}

HomeScreen.js;

import React from 'react';
import {Button, View, SafeAreaView, StyleSheet} from 'react-native';
import HeaderSB from "../component/SBHeader";
import FooterSB from "../component/SBFooter";

// import ShareExample from "./assets/component/SBShare";

export function HomeScreen({navigation}) {

    const receiveValue = (value) => {
        console.log("value received from B", value)
    }

    return (
        <>
            <SafeAreaView style={styles.container}>
                <View style={styles.headerContainer}>
                    <HeaderSB receiveValue={receiveValue}>
                    </HeaderSB>
                </View>
                <View style={styles.mainContent}>
                    <View style={styles.buttonBox}>
                        <Button onPress={() => navigation.navigate('Layout Props')} title="Layout Props"/>
                        <Button onPress={() => navigation.navigate('View Props')} title="View Style Props"/>
                    </View>
                </View>
                <View styles={styles.footerContainer}>
                    <FooterSB receiveValue={receiveValue}>
                    </FooterSB>
                </View>

            </SafeAreaView>
        </>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "space-between",
    },
    headerContainer: {

    },
    mainContent: {

    },
    buttonBox: {}
    ,
    footerContainer: {

    },
});

页脚.js;

import * as React from "react";
import {StyleSheet, TouchableOpacity, View} from "react-native";
import {AntDesign, Feather} from "@expo/vector-icons";
import {StackNavigation} from "./StackNavigation";

const FooterSB = ({navigation}) => {
    return (
        <>
            {/* Bottom App Navigation */}
            <View style={footerStyle.navCon}>
                <View style={footerStyle.navBar}>

                    {/* Home Button */}
                    <TouchableOpacity style={footerStyle.home} onPress={() =>
                        navigation.navigate('Home')}
                    >
                        {/* Icon */}
                        <AntDesign name="home" size={50} color="white"/>
                    </TouchableOpacity>

                    {/* Navigation Divider */}
                    <View style={footerStyle.navDivider}/>

                    {/* Setting Button */}
                    <TouchableOpacity style={footerStyle.settings} onPress={() =>
                        navigation.navigate('Layout Props')}
                    >
                        {/* Icon */}
                        <Feather name="settings" size={50} color="white"/>
                    </TouchableOpacity>
                </View>
            </View>
        </>

    );
}

const footerStyle = StyleSheet.create({
    navCon: {
        flex: 1,
        marginTop: 20,
        alignItems: "center",
        justifyContent: "flex-end"
    },
    navBar: {
        flexDirection: "row",
        justifyContent: "space-evenly",
        alignItems: "center",
        backgroundColor: "#808080",
        width: 500,
        height: 50,
    },
    home: {
        // paddingHorizontal: 55,
    },
    navDivider: {
        backgroundColor: "#FFF",
        width: 4,
        height: 50,
    },
    settings: {
        // marginRight: 107,
        // paddingHorizontal: 55,
    },
});

export default FooterSB;

【问题讨论】:

    标签: javascript reactjs react-native expo


    【解决方案1】:

    如果您想将导航作为默认道具,则应将其添加为 &lt;Stack.Screen&gt;,但由于 FooterSB 是自定义和单独的组件,它不会自动使用导航道具包装。

    您需要将导航作为道具传递给它,所以在下面进行更改

    <FooterSB receiveValue={receiveValue}>
    </FooterSB>
    

    <FooterSB receiveValue={receiveValue} navigation={navigation}>
    </FooterSB>
    

    您也可以将导航传递给标题。

    【讨论】:

    • 没用。 ¯_(ツ)_/¯
    • 现在的错误是什么?从 Stack.Screen 的名称中删除空格,即将“Layout Props”更改为“LayoutProps”
    • 仍然没有。错误是“找不到变量导航。“navigation.navigate”中的导航在它下面有一条波浪线,并希望我从 react-navigation 导入 NavigationActions 作为导航。我不使用它,但已经导入它,现在错误消失了,但是按下按钮没有任何动作。
    • 你必须在你的组件FooterSB和HeaderSB中都传递导航,我只是分享了FooterSB的例子。根据我的建议更新您的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2021-08-27
    • 2015-11-27
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多