我完全按照教程https://reactnavigation.org/docs/intro/ 但是标题没有显示出来。 这是代码和结果
import Expo from 'expo';
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import {StackNavigator} from 'react-navigation';
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
}
render() {
const {navigate} = this.props.navigation;
return (
<View style={styles.container}>
<Text>Open up main.js to start working on your app!</Text>
<Button onPress={()=>navigate('Chat',{user:'Lucy'})} title = 'Chat with Lucy'></Button>
</View>
);
}
}
class ChatScreen extends React.Component {
// Nav options can be defined as a function of the screen's props:
static navigationOptions = ({ navigation }) => ({
title: `Chat with ${navigation.state.params.user}`,
});
render() {
// The screen's current route is passed in to `props.navigation.state`:
const { params } = this.props.navigation.state;
return (
<View>
<Text>Chat with {params.user}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
const SimpleApp = StackNavigator({
Home: {screen: HomeScreen},
Chat: {screen: ChatScreen}
})
Expo.registerRootComponent(SimpleApp);
另一个问题是,如果我只使用
static navigationOptions = {
title: 'Chat with Lucy',
};
然后,&#34;欢迎&#34;仍然在标记旁边&#34;&lt;&#34;,这与教程不同。
答案 0 :(得分:6)
您使用的文档的版本比您安装的版本(similar issue on githib)更新。这是关于 npm 和 github 版本之间的区别。文件适用于github版本,这是较新的版本,但您从npm安装了react-navigation。
问题是您现在无法将navigationOptions
用作功能。当你这样做时,找不到navigationOptions
,所以不会有标题。请改为使用:
static navigationOptions = {
title: (navigation) => (`Chat with ${navigation.state.params.user}`),
};
当标题存在时,标题左侧不会显示上一页标题。
或更新您的package.json
,以便您可以使用 react-navigation 文档的版本:
"react-navigation": "git+https://github.com/react-community/react-navigation.git#7165efc",