3

I am trying to learn react native by doing a basic application. I followed many tutorials available online and made the following class for a login screen:

import React, { Component } from "react";
import { View, StyleSheet, TextInput, Button, Alert, TouchableHighlight, Text } from "react-native";
import { styles } from "../styles/CommonStyles";

class LoginScreen extends Component {
    constructor(props: Readonly<{}>) {
        super(props)
    }
    state = {
        email: ''
    }
    static navigationOptions = {
        title: 'Login',
        header: null
    }
    render() {
        const { navigate } = this.props.navigation
        return (
            <View style={styles.container}>

                <TextInput
                style={loginStyles.emailInput}
                multiline={false}
                autoCorrect={false}
                autoCapitalize='none'
                keyboardType='email-address'
                value={this.state.email}
                onChangeText={(text) => this.handleEmail(text)}
                placeholder='Enter Email'/>

                <TouchableHighlight
                style={loginStyles.buttonGenerateOTP}
                onPress={this.onGenerateOTP}>
                    <Text
                    style={loginStyles.textGenerateOTP}>GENERATE OTP</Text>
                </TouchableHighlight>
            </View>
        )
    }

    handleEmail(text: string) {
        console.log('Email: ' + text)
        this.setState({ email: text })
        console.log('State Email: ' + this.state.email)
    }

    onGenerateOTP() {
        Alert.alert(
            'Email Entered',
            this.state.email,
            [
                { text: 'OK', style: 'cancel' }
            ],
            { cancelable: false }
        )
    }
}

export default LoginScreen

const loginStyles = StyleSheet.create({
    emailInput: {
        fontSize: 15,
        backgroundColor: '#ECECEC',
        borderColor: '#999999',
        borderRadius: 4,
        borderWidth: 0.5,
        alignSelf: 'stretch',
        textAlign: 'center',
        margin: 10,
      },
      buttonGenerateOTP: {
          backgroundColor: '#008CBA',
          borderRadius: 4,
          alignSelf: 'stretch',
          justifyContent: 'center',
          alignItems: 'center',
          margin: 10,
          padding: 10,
    },
    textGenerateOTP: {
        fontSize: 20,
        color: '#FFFFFF',
    },
})

It only contains an input and a button, on which an alert should be shown with the entered email. But when I click the button, I am getting the following error:

undefined is not an object (evaluating 'this.state.email')
onGenerateOTP
LoginScreen.tsx:43:8
...

I am pretty new to react native and typescript, but looking at the code, I couldn't figure out any issues. What is it that I am doing wrong here?

EDIT The log in handleEmail(text) function is actually printing the state, although with the previous value of text(ie, when I enter QWERT, it will print QWER, and when I enter QWERTY, it will print QWERT), but the text param is logging correctly

2 Answers 2

1

I solved the issue. The issue was that I had to bind the functions. Changing the constructor as per below fixed the issue.

constructor(props: Readonly<{}>) {
    super(props)
    this.handleEmail = this.handleEmail.bind(this)
    this.onGenerateOTP = this.onGenerateOTP.bind(this)
}
Sign up to request clarification or add additional context in comments.

Comments

0

you have to call in your constructor or componentWillMount life-cycle method

this.setState{{'email': 'your_value}}

Only change the state through the setState method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.