【问题标题】:React native nested stack navigation showing empty screen反应原生嵌套堆栈导航显示空屏幕
【发布时间】:2022-01-11 09:30:46
【问题描述】:

我正在尝试使用来自网站的 example 之后使用 Stack navigator 实现嵌套反应原生导航。我还检查了 SO 中的其他问题,但找不到我的错误。

Home.tsx 组件中,我有两个嵌套路由 HomeView 和 Products。单击HomeView.tsx 中的链接时,我正在Home.tsx 中执行categoryClick,在此函数中,我想导航到Products.tsx 组件。导航按预期工作,但在 screen 中为 Products 组件定义的嵌套路由没有按预期工作。它显示一个空屏幕。请帮助理解我的错误

Home.tsx

import React, {useState} from "react";
import {View} from "react-native";
import Header from "../header/Header";
import HomeStyles from './Home.scss';

import {createNativeStackNavigator} from "@react-navigation/native-stack";
import Products from "../products/Products";
import HomeView from "../home-view/HomeView";

export interface HomeProps {}

export function Home({route, navigation}) {
  let [loader, setLoader] = useState(false)
  const Stack = createNativeStackNavigator()

  function categoryClick(e: number) {
    // expectation is it will navigate to `Ab` screen on loading `Products`
    navigation.navigate('Products', {
      screen: 'Ab',
      params: {}
    })
  }

  return (
    <View style={
      HomeStyles.homeContainer}>
      <View style={HomeStyles.homeHeader}>
        <Header/>
      </View>

      <Stack.Navigator initialRouteName='Products'>
        <Stack.Screen name='HomeView'
                      options={{headerShown: false}}>
          {(props) => <HomeView
            catrgotryClick={categoryClick}
            {...props} />}
        </Stack.Screen>
        <Stack.Screen
          name='Products'
          component={Products}
          options={{headerShown: false}}/>
      </Stack.Navigator>


    </View>
  );
}

export default Home;

Products.tsx

import { Route, Link } from 'react-router-dom';
import ProductsStyles from  './Products.scss';
import {Text, View} from "react-native";
import React, {useEffect, useState} from "react";
import {RouteProp, useNavigation} from "@react-navigation/native";
import Ab from "../ab/Ab";
import Hi from "../hi/Hi";
import Ac from "../ac/Ac";
import Je from "../je/Je";
import {createNativeStackNavigator} from "@react-navigation/native-stack";


export interface ProductsProps {
  route:RouteProp<any>;
  navigation:any
}


export function Products(props:ProductsProps) {
  const ProductStack = createNativeStackNavigator();
  return (
    <View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
     <ProductStack.Navigator>
       <ProductStack.Screen name='Ab' options={{headerShown:false}}
        component={Ab}/>
        <ProductStack.Screen name='Hi' component={Hi}/>
        <ProductStack.Screen name='Ac' component={Ac}/>
        <ProductStack.Screen name='Je' component={Je}/>
      </ProductStack.Navigator>
      </View>
 );
}

export default Products;

【问题讨论】:

    标签: javascript reactjs react-native react-navigation


    【解决方案1】:

    我们不能把 Navigator 放到 View 里面,所以你需要把 Products.tsx 文件改成

    import { Route, Link } from 'react-router-dom';
    import ProductsStyles from  './Products.scss';
    import {Text, View} from "react-native";
    import React, {useEffect, useState} from "react";
    import {RouteProp, useNavigation} from "@react-navigation/native";
    import Ab from "../ab/Ab";
    import Hi from "../hi/Hi";
    import Ac from "../ac/Ac";
    import Je from "../je/Je";
    import {createNativeStackNavigator} from "@react-navigation/native-stack";
    
    
    export interface ProductsProps {
      route:RouteProp<any>;
      navigation:any
    }
    
    
    export function Products(props:ProductsProps) {
      const ProductStack = createNativeStackNavigator();
      return (
         <ProductStack.Navigator>
           <ProductStack.Screen name='Ab' options={{headerShown:false}}
            component={Ab}/>
            <ProductStack.Screen name='Hi' component={Hi}/>
            <ProductStack.Screen name='Ac' component={Ac}/>
            <ProductStack.Screen name='Je' component={Je}/>
          </ProductStack.Navigator>
     );
    }
    
    export default Products;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2022-01-22
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多