I really need some assistance. I am attempting to create a JSON object called users in my state properties to test the functionality of my authentication system. However, when I tried to access it, I encountered the error "Cannot read property 'state' of undefined" with the error pointing to this part of the code const { users, textInputEmail, textInputPassword } = this.state. Additionally, when I checked the users variable, it showed 'undefined'. What did I do incorrectly?
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: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="177d786773676c72725b66737279797b6275387a7674">[email protected]</a>',
password: '12345678'
},
{
name: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="466f6a60657a686f48787e61636275233a3634">[email protected]</a>',
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>
)
}
}