我試圖在react-navigation
的幫助下在兩個屏幕之間導航。我能夠在render
方法內訪問navigate
,因爲其範圍也在該方法內。找不到變量:導航
我應該在哪裏申報,所以我可以訪問它component
的任何方法。我試圖在onPressButton
方法中訪問navigate
,但它給出錯誤。
找不到變量:導航
import React, { Component } from "react";
import { View, Text, Image, Button, Alert, StyleSheet } from "react-native";
import styles from "./Styles";
import * as strings from "./Strings";
import RoundButton from "./RoundButton";
var DialogAndroid = require("react-native-dialogs");
import { StackNavigator } from "react-navigation";
export default class CreateMessageScreen extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Image source={require("./img/create_message.png")} />
<Text style={styles.textStyle}>{strings.create_message}</Text>
<RoundButton
textStyle={styles.roundTextStyle}
buttonStyle={styles.roundButtonStyle}
onPress={this.onPressButton}
>
CREATE MESSAGE
</RoundButton>
</View>
);
}
onPressButton() {
var options = {
title: strings.app_name,
content: strings.create_message,
positiveText: strings.OK,
onPositive:() => navigate("DashboardScreen")
};
var dialog = new DialogAndroid();
dialog.set(options);
dialog.show();
}
}
試試這個const {navigation} = this.props; – Ved
@Ved我在'render'方法中有'const {navigate} = this.props.navigation;' –
是的。我知道。你能測試我所問的嗎?我會比較詳細地更新答案。 – Ved