React Native TypeScript TextInput不起作用

时间:2019-06-22 07:48:51

标签: typescript react-native react-native-textinput react-native-component

我正在尝试通过做一个基本的应用程序来学习本机响应。我遵循了许多在线教程,并为登录屏幕创建了以下课程:

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',
    },
})

它仅包含一个输入和一个按钮,应该在其上显示带有输入电子邮件的警报。但是当我单击按钮时,出现以下错误:

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

我是本机和打字稿的新手,但是看着代码,我看不出任何问题。我在这里做错什么了?

编辑 handleEmail(text)函数的登录实际上是在打印状态,尽管使用文本的先前值(即,当我输入QWERT时,它将打印QWER,而当我输入{{1 }},它将打印QWERTY),但文本参数正确记录了

2 个答案:

答案 0 :(得分:0)

您必须调用构造函数或componentWillMount生命周期方法

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

仅通过setState方法更改状态

答案 1 :(得分:0)

我解决了这个问题。问题是我必须绑定功能。如下更改Changinf构造函数即可解决此问题。

constructor(props: Readonly<{}>) {
    super(props)
    this.handleEmail = this.handleEmail.bind(this)
    this.onGenerateOTP = this.onGenerateOTP.bind(this)
}