gpt4 book ai didi

javascript - this.props.navigation 未定义(createStackNavigator)

转载 作者:行者123 更新时间:2023-11-30 19:03:27 24 4
gpt4 key购买 nike

我想在 ReactNative 上创建一个简单的导航示例。

下面是一段代码;

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';

class Home extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home</Text>
<Button
title="To the detail Page"
onPress={() => this.props.navigation.navigate('DetailScreen')}
/>
</View>
);
}
}

class Detail extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Detail Page</Text>
</View>
);
}
}

export default createStackNavigator({
HomeScreen: { screen: Home },
DetailScreen: { screen: Detail },
})

当我像这样编写代码时,会发生错误,因为第 12 行 this.props.navigation 未定义。

有没有人有解决办法?

最佳答案

请尝试下面的代码:

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

createAppContainer
} from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';

class Home extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home</Text>
<Button
title="To the detail Page"
onPress={() => this.props.navigation.navigate('DetailScreen')}
/>
</View>
);
}
}

class Detail extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Detail Page</Text>
</View>
);
}
}

const APpStack = createStackNavigator({
HomeScreen: { screen: Home },
DetailScreen: { screen: Detail },
})

const App = createAppContainer(APpStack);

export default App;

您还可以查看工作解决方案 link expo

希望对你有帮助。有疑问请随意

关于javascript - this.props.navigation 未定义(createStackNavigator),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246000/

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