react-navigation动态标头不起作用?

Joh*_*ohn 3 react-native react-navigation expo

我完全遵循教程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);
Run Code Online (Sandbox Code Playgroud)

这是我点击按钮时的屏幕结果 在此输入图像描述

另一个问题是,如果我只使用

static navigationOptions = {
    title: 'Chat with Lucy',
  };
Run Code Online (Sandbox Code Playgroud)

然后,"欢迎"仍然在标记"<"旁边,这与教程不同.

在此输入图像描述

Mey*_*ehr 6

您使用的文档的版本比您安装的版本更新(githib上的类似问题).这是关于npmgithub版本之间的区别.文件适用于github版本,这是较新的版本,但您从npm安装了react-navigation.

问题是你现在不能navigationOptions用作功能.当你这样做它找不到navigationOptions,所以没有标题.改为使用:

static navigationOptions = {
  title: (navigation) => (`Chat with ${navigation.state.params.user}`),
};
Run Code Online (Sandbox Code Playgroud)

当标题存在时,标题左侧不会显示上一页标题.

或者更新您的package.json,以便您可以使用react-navigation文档的版本:

"react-navigation": "git+https://github.com/react-community/react-navigation.git#7165efc",
Run Code Online (Sandbox Code Playgroud)