“React Navigation (Native) navigation.navigate” 不起作用,抛出未定义错误。

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

React Navigation (Native) navigation.navigate isn't working and throwing an undefined error

问题

我的自定义按钮应该带我回到主页,但没有并显示一个“未定义错误”。我相当确定这是因为堆栈导航器的东西在主文件中,但我不知道如何让这个代码文件访问它,我的谷歌搜索和文档查阅都没有成功。对于这个简单问题的任何帮助将不胜感激。

英文:

My Custom Button that should take me home doesn't and give me an "undefined error." I'm pretty sure its because the Stack Navigator stuff is in the main file, but I don't know how to give this code file access to it and my Google efforts and the Docs have been unsuccessful. Any help on this simple issue would be appreciated.

import React from 'react';
import { View, Text, Image, StyleSheet } from 'react-native';
import CustomButton from './CustomButton';
import { useFonts } from 'expo-font';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import player from './Player';
import App from './App';
import usePotion from './UsePotion';
import { useState } from 'react';
import { useEffect } from 'react';

    

function BattleScreen({ navigation }) {
    
    const [fontsLoaded, error] = useFonts({
        'Valorax': require('./Fonts/Valorax.otf'),
      });
    
      if (!fontsLoaded) {
        return <Text>Loading...</Text>;
      }


    return (
      <View style={styles.container}>
        <Text style={styles.text}>{player.potions}</Text>
        <View style={styles.topHalf}>
        </View>
        <View style={styles.bottomHalf}>
          <View style={styles.buttonRow}>
            <CustomButton onPress={() => handleAttack()} title='Attack'></CustomButton>
            <CustomButton onPress={() => handleMagic()} title='Magic'></CustomButton>
          </View>
          <View style={styles.buttonRow}>
            <CustomButton onPress={usePotion()} title='Use Potion'></CustomButton>
            <CustomButton onPress={() => handleRun()} title='Run'></CustomButton>
            <CustomButton onPress={() => navigation.navigate('Home')} title="Home"></CustomButton>
          </View>
        </View>
      </View>
    );
  }
  
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: '#000000',
    },
    topHalf: {
      flex: 1,
      color: 'white',
    },
    bottomHalf: {
      flex: 1,
      flexDirection: 'row',
      flexWrap: 'wrap'
    },
    buttonRow: {
      flex: 1,
      flexDirection: 'row',
      justifyContent: 'space-evenly',
      flexWrap: 'wrap'
    },
    text: {
        fontSize: 90,
        color: 'white',
        fontFamily: "Valorax",
    }
  });


export default BattleScreen;
**strong text**

答案1

得分: 0

使用useNavigation钩子(https://reactnavigation.org/docs/use-navigation/):

import { useNavigation } from '@react-navigation/native';

function BattleScreen() {
    const [fontsLoaded, error] = useFonts({
        'Valorax': require('./Fonts/Valorax.otf'),
    });
    const navigation = useNavigation()

    if (!fontsLoaded) {
        return <Text>Loading...</Text>;
    }

    return (
        <View style={styles.container}>
            <Text style={styles.text}>{player.potions}</Text>
            <View style={styles.topHalf}>
            </View>
            <View style={styles.bottomHalf}>
                <View style={styles.buttonRow}>
                    <CustomButton onPress={() => handleAttack()} title='Attack'></CustomButton>
                    <CustomButton onPress={() => handleMagic()} title='Magic'></CustomButton>
                </View>
                <View style={styles.buttonRow}>
                    <CustomButton onPress={usePotion()} title='Use Potion'></CustomButton>
                    <CustomButton onPress={() => handleRun()} title='Run'></CustomButton>
                    <CustomButton onPress={() => navigation.navigate('Home')} title="Home"></CustomButton>
                </View>
            </View>
        </View>
    );
}
英文:

Try using the useNavigation hook (https://reactnavigation.org/docs/use-navigation/):

import { useNavigation } from &#39;@react-navigation/native&#39;;

function BattleScreen() {
    const [fontsLoaded, error] = useFonts({
        &#39;Valorax&#39;: require(&#39;./Fonts/Valorax.otf&#39;),
      });
      const navigation = useNavigation()
    
      if (!fontsLoaded) {
        return &lt;Text&gt;Loading...&lt;/Text&gt;;
      }


    return (
      &lt;View style={styles.container}&gt;
        &lt;Text style={styles.text}&gt;{player.potions}&lt;/Text&gt;
        &lt;View style={styles.topHalf}&gt;
        &lt;/View&gt;
        &lt;View style={styles.bottomHalf}&gt;
          &lt;View style={styles.buttonRow}&gt;
            &lt;CustomButton onPress={() =&gt; handleAttack()} title=&#39;Attack&#39;&gt;&lt;/CustomButton&gt;
            &lt;CustomButton onPress={() =&gt; handleMagic()} title=&#39;Magic&#39;&gt;&lt;/CustomButton&gt;
          &lt;/View&gt;
          &lt;View style={styles.buttonRow}&gt;
            &lt;CustomButton onPress={usePotion()} title=&#39;Use Potion&#39;&gt;&lt;/CustomButton&gt;
            &lt;CustomButton onPress={() =&gt; handleRun()} title=&#39;Run&#39;&gt;&lt;/CustomButton&gt;
            &lt;CustomButton onPress={() =&gt; navigation.navigate(&#39;Home&#39;)} title=&quot;Home&quot;&gt;&lt;/CustomButton&gt;
          &lt;/View&gt;
        &lt;/View&gt;
      &lt;/View&gt;
    );
  }

huangapple
  • 本文由 发表于 2023年1月6日 10:55:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026503.html
匿名

发表评论

匿名网友

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

确定