React native nested stack navigation showing empty screen(反应显示空屏幕的本机嵌套堆栈导航)
本文介绍了反应显示空屏幕的本机嵌套堆栈导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用堆栈导航器实现来自网站的example嵌套的Reaction本机导航。我还签入了其他问题SO,但找不到我的错误。
在Home.tsx
组件I中,有两个嵌套的路由HomeView和Products。单击HomeView.tsx
中的链接,我正在执行categoryClick
中的categoryClick
,在此函数中,我想导航到Products.tsx
组件。导航按预期工作,但screen
中为产品组件定义的嵌套路由未按预期工作。它正在显示一个空屏幕。请帮助理解我的错误
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;
推荐答案
我们无法将导航器放入视图中,因此您需要将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;
这篇关于反应显示空屏幕的本机嵌套堆栈导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:反应显示空屏幕的本机嵌套堆栈导航
基础教程推荐
猜你喜欢
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 响应更改 div 大小保持纵横比 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01