2

我正在努力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>;
  }
}

在此处输入图像描述

4

2 回答 2

6

在您的EnableNotification.js中,您在没有默认值的情况下导出您的EnableNotification类(这是一个命名导出)。

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

你应该

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

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

在此处阅读有关出口类型的更多信息

于 2017-04-24T13:56:09.233 回答
0

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

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

像这样注册您的应用程序:

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

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

const ScheduledApp = StackNavigator({
  Splash: { screen: SplashScreen },
  EnableNotification: { screen: EnableNotification }
}, {
  initialRouteName: 'Splash'
});
于 2017-04-21T13:27:59.227 回答