路线'启用通知'应该宣布一个屏幕

时间:2017-04-18 11:08:28

标签: android reactjs react-native react-navigation

我正在使用react-native并尝试整合react-navigation https://reactnavigation.org/docs/intro/进行导航。我在实施时遇到了一些困难。

index.android.js

**
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from "react";
import {
  AppRegistry,
  Image,
  View,
  Text,
  Button,
  StyleSheet
} from "react-native";
import { StackNavigator } from "react-navigation";
import EnableNotificationScreen from "./EnableNotification";

class SplashScreen extends Component {
  render() {
    console.disableYellowBox = true;
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <Image source={require("./img/talk_people.png")} />
        <Text style={{ fontSize: 22, textAlign: "center" }}>
          Never forget to stay in touch with the people that matter to you.
        </Text>
        <View style={{ width: 240, marginTop: 30 }}>
          <Button
            title="CONTINUE"
            color="#FE434C"
            onPress={() => navigate("EnableNotification")}
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: "#FFFFFF",
    alignItems: "center",
    justifyContent: "center",
    padding: 16,
    flex: 1,
    flexDirection: "column"
  }
});

const ScheduledApp = StackNavigator(
  {
    Splash: { screen: SplashScreen },
    EnableNotification: { screen: EnableNotificationScreen }
  },
  {
    initialRouteName: "Splash"
  }
);

AppRegistry.registerComponent("Scheduled", () => ScheduledApp);

EnableNotification.js

/**
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from "react";
import { View, Text } from "react-native";

export class EnableNotification extends Component {
  render() {
    return <View><Text>Enable Notification</Text></View>;
  }
}

enter image description here

2 个答案:

答案 0 :(得分:6)

EnableNotification.js中,您可以导出EnableNotification类而不使用默认值(这是一个命名导出)。

然后您在import EnableNotificationScreen from "./EnableNotification"中使用index.android.js导入它,这会导致错误。

你应该

a)导出默认的EnableNotification屏幕,即export default class EnableNotification extends Component

b)更改为import { EnableNotification } from "./EnableNotification"

详细了解导出类型here

答案 1 :(得分:0)

现在您正在注册Scheduled组件。您应该做的是注册ScheduledApp组件。

目前未使用ScheduledApp,因此无法找到您导航到的EnableNotification屏幕。

像这样注册您的应用:

AppRegistry.registerComponent("Scheduled", () => ScheduledApp);

我也经常做的是定义初始路线。你可以通过定义initialRouteName来做到这一点:

const ScheduledApp = StackNavigator({
  Splash: { screen: SplashScreen },
  EnableNotification: { screen: EnableNotification }
}, {
  initialRouteName: 'Splash'
});