【问题标题】:how to can navigate to another screen in react-native如何在 react-native 中导航到另一个屏幕
【发布时间】:2021-04-09 11:22:58
【问题描述】:

当用户单击时,我正在尝试导航到我的相册组件中的另一个屏幕 在专辑中,但我不断收到此错误

The action 'NAVIGATE' with payload {"name":"AlbumScreen"} was not handled by any navigator.

下面是我的代码和我的尝试

导航->相册->index.tsx

import React from 'react';
import { View, Image, Text, TouchableWithoutFeedback } from 'react-native';
import styles from './style';
import { Album, TabOneParamList } from '../../types';
import { useNavigation } from '@react-navigation/native';
// import Navigation from '../navigation';

export type AlbumProps = {
    album: Album,

}

const AlbumComponent = (props: AlbumProps) => {

    const navigation = useNavigation();
    const onPress = () => {

        navigation.navigate('AlbumScreen');
    }



    return (
        <TouchableWithoutFeedback onPress={onPress}>
            <View style={styles.container}>
                <Image source={{ uri: props.album.imageUri }} style={styles.image} />
                <Text style={styles.text}>{props.album.artistsHeadline}</Text>
            </View>
        </TouchableWithoutFeedback>
    );
}






export default AlbumComponent;

这是我的 AlbumScreen.tsx,这也是我希望用户在点击相册后移动到的屏幕

import React from 'react';

import { View, Text } from 'react-native';

const AlbumScreen = () => (
    <View>
        <Text style={{ color: 'white' }} >Hello from Album Screen</Text>
    </View>
);



export default AlbumScreen;

这也是我的 BottomTabNavigation,我添加新创建的 AlbumScreen 就像添加 HomeScreen 一样

import {
  Ionicons,
  Entypo,
  EvilIcons,
  MaterialCommunityIcons,
  FontAwesome5,
  FontAwesome
} from '@expo/vector-icons';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { createStackNavigator } from '@react-navigation/stack';
import * as React from 'react';

import Colors from '../constants/Colors';
import useColorScheme from '../hooks/useColorScheme';
import TabOneScreen from '../screens/HomeScreen';
import AlbumScreen from '../screens/AlbumScreen';
import TabTwoScreen from '../screens/TabTwoScreen';
import { BottomTabParamList, TabOneParamList, TabTwoParamList } from '../types';

const BottomTab = createBottomTabNavigator<BottomTabParamList>();

export default function BottomTabNavigator() {
  const colorScheme = useColorScheme();

  return (
    <BottomTab.Navigator
      initialRouteName="Home"
      tabBarOptions={{ activeTintColor: Colors[colorScheme].tint }}>
      <BottomTab.Screen
        name="Home"
        component={TabOneNavigator}
        options={{
          tabBarIcon: ({ color }) => <Entypo name="home" size={30} style={{ marginBottom: -3 }} color={color} />,
        }}
      />
      <BottomTab.Screen
        name="store"
        component={TabTwoNavigator}
        options={{
          tabBarIcon: ({ color }) => <MaterialCommunityIcons name="store" size={30} style={{ marginBottom: -3 }} color={color} />,
        }}
      />
      <BottomTab.Screen
        name="Library"
        component={TabTwoNavigator}
        options={{
          tabBarIcon: ({ color }) => <Ionicons name="library-outline" size={30} style={{ marginBottom: -3 }} color={color} />,
        }}
      />
      <BottomTab.Screen
        name="Profile"
        component={TabTwoNavigator}
        options={{
          tabBarIcon: ({ color }) => <FontAwesome name="user-circle" size={28} style={{ marginBottom: -3 }} color={color} />,
        }}
      />
    </BottomTab.Navigator>
  );
}



// Each tab has its own navigation stack, you can read more about this pattern here:
// https://reactnavigation.org/docs/tab-based-navigation#a-stack-navigator-for-each-tab
const TabOneStack = createStackNavigator<TabOneParamList>();

function TabOneNavigator() {
  return (
    <TabOneStack.Navigator>
      <TabOneStack.Screen
        name="TabOneScreen"
        component={TabOneScreen}
        options={{ headerTitle: 'Home' }}
      />
       <TabOneStack.Screen
        name="AlbumScreen"
        component={AlbumScreen}
        options={{ headerTitle: 'Album' }}
      />
    </TabOneStack.Navigator>
  );
}

const TabTwoStack = createStackNavigator<TabTwoParamList>();

function TabTwoNavigator() {
  return (
    <TabTwoStack.Navigator>
      <TabTwoStack.Screen
        name="TabTwoScreen"
        component={TabTwoScreen}
        options={{ headerTitle: 'Tab Two Title' }}
      />
    </TabTwoStack.Navigator>
  );
}

这是我想要执行的操作的图片,单击此图片右侧的相册后,用户将直接进入另一个屏幕,但我不断收到突出显示的错误my errors

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    您正在导航到一个名为“AlbumScreen”的屏幕,该屏幕不存在。这就是您收到此错误的原因。

    import React from 'react';
    import { View, Image, Text, TouchableWithoutFeedback } from 'react-native';
    import styles from './style';
    import { Album, TabOneParamList } from '../../types';
    import { useNavigation } from '@react-navigation/native';
    // import Navigation from '../navigation';
    
    export type AlbumProps = {
        album: Album,
    }
    
    const AlbumComponent = (props: AlbumProps) => {
        const navigation = useNavigation();
        const onPress = () => {
            navigation.navigate('Home'); 
            // Here was the error..As you can see "AlbumScreen" does not exist in Bottom Navigator that you are returning. So Either replace "AlbumScreen" with "Home" or place another route with name "AlbumScreen" in Bottom Navigator
        }
        return (
            <TouchableWithoutFeedback onPress={onPress}>
                <View style={styles.container}>
                    <Image source={{ uri: props.album.imageUri }} style={styles.image} />
                    <Text style={styles.text}>{props.album.artistsHeadline}</Text>
                </View>
            </TouchableWithoutFeedback>
        );
    }
    
    export default AlbumComponent;
    

    【讨论】:

    • 但我创建了这个屏幕
    • 但我创建了这个屏幕
    • 是的,您已经创建了AlbumScreen.js,但它不在&lt;BottomTab.Navigator&gt; 部分检查一次.. 所以导航的可能路线是Home store Library Profile 我会推荐你可以使用Stack Navigator 来处理你的情况...见this
    最近更新 更多