I am trying to navigate between two screens using react-navigation . I can access navigate inside the render method, since its scope is also inside this method.
Where should I declare, so I can access it with any method of this component . I am trying to access navigate inside the onPressButton method, but it gives an error.
Unable to find variable: 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(); } }
source share