gpt4 book ai didi

javascript - React Native - 导入自定义组件时出现导航错误

转载 作者:行者123 更新时间:2023-11-28 14:23:52 25 4
gpt4 key购买 nike

我正在尝试在这里制作自定义组件(页脚)。这是我的代码:

Footer.js

    import React, { Component } from 'react'
import {
View,
Text,
Linking,
StyleSheet,
Image,
TouchableOpacity
Dimensions,
} from 'react-native'

class Footer extends Component {
constructor(props) {
super(props);
}

static navigationOptions = ({ navigation }) => ({
title: ''
})

_pageAbout = () => {
this.props.navigation.navigate('About');
}

render() {
return (
<View>
<View style={{ margin: 15 }}>
<TouchableOpacity
onPress={()=>this._pageAbout()}>
<Text>About</Text>
</TouchableOpacity>
</View>
</View>

);

}
}

export default Footer

然后我在 Home.js 屏幕上导入并调用该组件。Home.js

import React, { Component } from 'react'
import {
View,
Text,
Linking,
StyleSheet,
Image,
TouchableOpacity
Dimensions,
} from 'react-native'
class Home extends Component {
constructor(props) {
super(props);
}

static navigationOptions = ({ navigation }) => ({
title: ''
})

render() {
return (
<Footer />
);
}
}

export default Home

但是当我单击“关于”文本时,它会返回

TypeError: undefined is not an object (evaluating this.props.navigation.navigate) in react native

请帮忙。先谢谢了。

最佳答案

您需要将导航作为 Prop 传递给页脚

<Footer navigation={this.props.navigation} />   

由于页脚将不知道导航堆栈

关于javascript - React Native - 导入自定义组件时出现导航错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54351165/

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