gpt4 book ai didi

javascript - 即使遵循 react 导航文档, react 导航也无法在屏幕之间导航

转载 作者:行者123 更新时间:2023-11-30 10:58:33 25 4
gpt4 key购买 nike

我正在关注 this doc 从 react-navigation 学习,我正在尝试和尝试,但我无法在屏幕之间导航。每次我更改某些内容时,都会弹出一个随机错误。如果我只调用一个屏幕作为 <HomeScreen/>,App.js 运行良好在 App.js 的根目录中。

我这样调用 HomeScreen:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import HomeScreen from './Comps/HomeScreen';
import { StackNavigator } from 'react-navigation';
import DetailsScreen from './Comps/DetailsScreen';

class App extends React.Component {
render() {
return (
<HomeScreen />
);
}
}



const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

export default App;

现在一切正常,显示了里面的内容 HomeScreen.js

 import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, Button } from 'react-native';
import { createDrawerNavigator } from 'react-navigation-drawer';
import { createAppContainer, navigation } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';

class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('Details')}
/>
</View>
);
}
}

export default HomeScreen;

但是当我点击按钮时,随机弹出一个错误,有时它说 undefined is not an object,有时它找不到 StackNavigator ,有时导航有问题。我安装了所有依赖项,但如果我在这里遗漏了什么,请告诉我。

来自 HomeScreen.js我调用DetailsScreen.js就像文档中说的那样。

 import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, Button } from 'react-native';
import { createDrawerNavigator } from 'react-navigation-drawer';
import { createAppContainer, navigation } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { withNavigation } from 'react-navigation';


class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button
title="Go to Details... again"
onPress={() => this.props.navigation.navigate('Details')}
/>
</View>
);
}
}
export default DetailsScreen;

最佳答案

您需要像这样使用 createAppContainer 定义 AppContainer( What is AppContainers )

const AppContainer = createAppContainer(RootStack);

RootStack 将是您要显示的屏幕列表。使用 createStackNavigator( what is createStackNavigator ) 像这样添加它们:

const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);

并在 App.js 中添加 const AppContainerconst RootStack,然后像这样导出 class App:

 export default class App extends React.Component {
render() {
return <AppContainer/>;
}

如果屏幕在不同的文件中,比如 ./Comps/HomeScreen.js./Comps/DetailsS​​creen.js 然后在创建之前将它们导入 App.js屏幕。像这样:

import HomeScreen from './Comps/HomeScreen';
import DetailsScreen from './Comps/DetailsScreen'

关于javascript - 即使遵循 react 导航文档, react 导航也无法在屏幕之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59014360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com