【问题标题】:Why I get error "Cannot read property 'state' of undefied in react-native? [duplicate]为什么我收到错误“无法读取 react-native 中未定义的属性‘状态’?[重复]
【发布时间】:2021-09-12 22:26:16
【问题描述】:

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

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

【问题讨论】:

  • 尝试将isUser() { 更改为isUser = () =&gt; {。否则,您将需要在构造函数中绑定isUser。见this
  • (有人能找到典型的骗子吗?那是我目前能做的最好的了。)

标签: javascript json react-native dictionary react-native-android


【解决方案1】:

当你这样做时:

onPress={this.isUser}

您将isUser 从组件的范围中分离出来,当它被自己调用时,“this”是未定义的。你可以通过创建一个只调用this.isUser()的箭头函数来修复它:

onPress={() => this.isUser()}

或将isUser 本身设为箭头函数:

isUser = () => {
  ...
}

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

【讨论】:

    【解决方案2】:

    在 React 类组件中,您需要 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)} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-02
      • 2018-10-22
      • 2020-08-31
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多