react-navigation 动态 header 不起作用?
react-navigation dynamic header doesn't work?
我完全按照教程做了 https://reactnavigation.org/docs/intro/
但是 header 没有出现。
这是代码和结果
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',
};
那么,"Welcome"后面还是“<”,跟教程不一样
您使用的文档版本比您安装的版本 (similar issue on githib) 更新。这是关于 npm 和 github 版本之间的区别。文档适用于更新的 github 版本,但您从 npm 安装了 react-navigation。
问题是您现在无法将 navigationOptions
用作函数。当您这样做时,它找不到 navigationOptions
,因此不会有 header。改用这个:
static navigationOptions = {
title: (navigation) => (`Chat with ${navigation.state.params.user}`),
};
当标题存在时,上一页标题将不会显示在 header 的左侧。
或更新您的 package.json
,以便您可以使用 react-navigation 文档的版本:
"react-navigation": "git+https://github.com/react-community/react-navigation.git#7165efc",
我完全按照教程做了 https://reactnavigation.org/docs/intro/ 但是 header 没有出现。 这是代码和结果
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',
};
那么,"Welcome"后面还是“<”,跟教程不一样
您使用的文档版本比您安装的版本 (similar issue on githib) 更新。这是关于 npm 和 github 版本之间的区别。文档适用于更新的 github 版本,但您从 npm 安装了 react-navigation。
问题是您现在无法将 navigationOptions
用作函数。当您这样做时,它找不到 navigationOptions
,因此不会有 header。改用这个:
static navigationOptions = {
title: (navigation) => (`Chat with ${navigation.state.params.user}`),
};
当标题存在时,上一页标题将不会显示在 header 的左侧。
或更新您的 package.json
,以便您可以使用 react-navigation 文档的版本:
"react-navigation": "git+https://github.com/react-community/react-navigation.git#7165efc",