0

我正在尝试使用 react-native 导航https://reactnavigation.org/在两个屏幕之间导航。它从 to 开始工作,index.js但从toEnableNotification.js不工作EnableNotification.jsCreateMessage.js

EnableNotification.js

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

import React, { Component } from "react";
import { View, Text, Image, Button, StyleSheet } from "react-native";
import { StackNavigator } from "react-navigation";
import styles from "./Styles";
import * as strings from "./Strings";
import CreateMessage from "./CreateMessage";

export default class EnableNotificationScreen extends Component {
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <Image source={require("./img/enable_notification.png")} />
        <Text style={styles.textStyle}> {strings.enable_notification} </Text>
        <View style={{ width: 240, marginTop: 30 }}>
          <Button
            title="ENABLE NOTIFICATION"
            color="#FE434C"
            onPress={() => navigate("CreateMessage")}
            style={{ borderRadius: 40 }}
          />
        </View>
        <Text
          style={{
            textAlign: "center",
            marginTop: 10
          }}
        >
          NOT NOW
        </Text>
      </View>
    );
  }
}

const ScheduledApp = StackNavigator({
  CreateMessage: {
    screen: CreateMessage,
    navigationOptions: {
      header: {
        visible: false
      }
    }
  }
});

CreateMessage.js

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

export default class CreateMessage extends Component {
  render() {
    return <View><Text>Hello World!</Text></View>;
  }
}
4

2 回答 2

0

第一的 :

index.android.jsindex.ios.js

import React, { Component } from 'react';
import { AppRegistry, Navigator } from 'react-native';

import Index from './app/Index';
import CreateMessage from './app/CreateMessage';
import EnableNotification from './app/EnableNotification';

render() {
 return (
  <Navigator
    initialRoute={{screen: 'Index'}}
    renderScene={(route, nav) => {return this.renderScene(route, nav)}}
  />
 )
}

renderScene(route,nav) {
 switch (route.screen) {
  case "Index":
    return <Index navigator={nav} />
  case "EnableNotification":
    return <EnableNotification navigator={nav} />
  case "CreateMessage":
    return <CreateMessage navigator={nav} />
 } 
}

在那之后 :

index.js

goEnableNotification() {
    this.props.navigator.push({ screen: 'EnableNotification' });
  }

最后 :

EnableNotification.js

goCreateMessage() {
    this.props.navigator.push({ screen: 'CreateMessage' });
  }

如果你想回去,做一个功能 goBack :

goBack() {
  this.props.navigator.pop();
}

我希望这能帮到您 !

于 2017-04-25T15:54:09.427 回答
0

这对我有用 -CreateMessage组件需要成为导航堆栈的一部分才能在其中导航this.props.navigator.navigate(<name>)

于 2017-04-27T07:21:48.133 回答