为什么我在 react-native 中收到错误消息“无法读取 undefied 的 属性 'state'?

Why I get error "Cannot read property 'state' of undefied in react-native?

我非常需要你的帮助。我正在尝试在我的状态属性中创建一个 JSON 对象用户来检查我的身份验证是如何工作的。但是当我试图获得访问权限时,我收到了错误“无法读取 属性 'state' of undefied”,错误箭头指向这部分代码 const { users, textInputEmail, textInputPassword } = this.state。此外,当我尝试检查用户时,它也显示 'undefined'。我做错了什么?

import React, { Component } from 'react'
import { View, TextInput } from 'react-native'
import { MyButton, ErrorMessage } from '../uikit'
import { FormStyle, InputStyle } from '../constants/styles'
import { SIGN_IN, SUCCESS } from '../routes'

export class LogIn extends Component {

    state = {
        users: {
            user: [
                {
                    name: 'john1303@ukr.net',
                    password: '12345678'
                },

                {
                    name: 'steve13@gmail.com',
                    password: '87654321'
                }

            ],
        },
        textInputEmail: '',
        textInputPassword: ''
    }

    isUser() {
        console.log(users)
        const { users, textInputEmail, textInputPassword } = this.state
        let currentUser, password;

        currentUser = users.map((user) => user.name == textInputEmail ? user : 'unknown')

        if (currentUser == 'unknown')
            return alert('Incorrect user or password!')
        else {
            if (currentUser.password == textInputPassword)
                this.props.navigation.navigate(SUCCESS)
        }
    }

    render() {
        const { mainContainer, buttons } = FormStyle
        const { container, text } = InputStyle

        return (
            <View>
                <View style={mainContainer}>
                    <View style={container}>
                        <TextInput
                            style={text}
                            placeholder={'Email/Login'}
                            onChangeText={(value) => this.setState({ textInputEmail: value })}
                        >
                        </TextInput>
                        <ErrorMessage errorText={'Incorrect email'} />
                    </View>

                    <View style={container}>
                        <TextInput
                            style={text}
                            placeholder={'Password'}
                            secureTextEntry={true}
                            onChangeText={(value) => this.setState({ textInputPassword: value })}
                        >
                        </TextInput>
                        <ErrorMessage errorText={'Incorrect password'} />
                    </View>

                    <View style={buttons}>
                        <MyButton
                            name={'Log in'.toUpperCase()}
                            onPress={this.isUser} />
                        <MyButton
                            name={'Sign in'.toUpperCase()}
                            onPress={() => this.props.navigation.navigate(SIGN_IN)} />
                    </View>
                </View>
            </View>
        )
    }
}

当你这样做时:

onPress={this.isUser}

您正在从组件的作用域中分离 isUser,当它自己被调用时,“this”是未定义的。您可以通过创建一个仅调用 this.isUser():

的箭头函数来修复它
onPress={() => this.isUser()}

或者通过使 isUser 本身成为箭头函数:

isUser = () => {
  ...
}

如果您对它的不同之处及其重要性的具体细节感兴趣,我已经在其他地方解释过

在 React class 组件中,您需要 bind 函数以获得正确的 this 值。有几种方法可以做到这一点,请参阅 here

您可以更改您的 isUser 声明:

isUser() {
  ...
}

收件人:

isUser = () => {
  ...
}

或者您可以通过添加如下构造函数来绑定它:

export class LogIn extends Component {
  constructor(props) {
    super(props);
    this.isUser = this.useUser.bind(this);
  }
  ...
}

或者您可以直接在 render 中绑定函数,如下所示:

<MyButton
  name={'Log in'.toUpperCase()}
  onPress={this.isUser.bind(this)} />