【发布时间】: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 = () => {。否则,您将需要在构造函数中绑定isUser。见this -
(有人能找到典型的骗子吗?那是我目前能做的最好的了。)
标签: javascript json react-native dictionary react-native-android