gpt4 book ai didi

javascript - React Native 嵌套导航 - 获取导航上下文

转载 作者:行者123 更新时间:2023-11-28 04:16:09 26 4
gpt4 key购买 nike

我正在尝试构建一个简单的嵌套导航器,即堆栈导航中的选项卡导航。我得到:

"undefined is not an object- evaluating this.props.navigation"

当尝试通过下面的“myFunction”导航堆栈时。我尝试了另一种方法,即通过导航选项的“headerright”实现堆栈导航,并且它有效。但我真的很想通过内容部分中的按钮来实现它。代码如下,显示了两种导航方法:

import React, { Component } from 'react';
import {
AppRegistry,
Text, View, Button, Image, StyleSheet
} from 'react-native';
import { TabNavigator, StackNavigator } from "react-navigation";

class RecentChatsScreen extends React.Component {

static navigationOptions = ({ navigation }) => ({
title: `Recent Chats`,
headerRight: <Button title="Go Chat" onPress={() => navigation.navigate('Chat', { user: 'Lucy' })}/>,
});

myFunction() {
const { navigate } = this.props.navigation;
navigate('Chat', { user: 'Lucy' });
}

render() {

return (
<View>
<Text>List of recent chats</Text>
<Button
onPress={
this.myFunction
}
title="Go Chat."
/>
</View>
);
}
}

class ChatScreen extends React.Component {
static navigationOptions = {
title: 'chat screen',
};

render() {
return <Text>Chat screen</Text>
}
}

class AllContactsScreen extends React.Component {
render() {
return <Text>List of all contacts</Text>
}
}

const MainScreenNavigator = TabNavigator({
Recent: { screen: RecentChatsScreen },
All: { screen: AllContactsScreen },
});

MainScreenNavigator.navigationOptions = {
title: 'My Chats',
};

const SimpleApp = StackNavigator({
Home: { screen: MainScreenNavigator },
Chat: { screen: ChatScreen },
});

AppRegistry.registerComponent('SimpleApp', () => SimpleApp);

最佳答案

您可能忘记绑定(bind)this,因此在调用myFunction时,它不在类RecentChatsScreen范围内。

尝试添加这个

class RecentChatsScreen extends React.Component {
constructor(props) {
super(props);
this.myFunction = this.myFunction.bind(this);
}
}

关于javascript - React Native 嵌套导航 - 获取导航上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45830009/

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