DEV Community

Federico Matias Bais
Federico Matias Bais

Posted on

condition inside map array - React Native

Hi i want to login with a local data array but i cant get an error alert when the username and password is worng. please can you guide me?

constructor (props) {
    super(props);
    this.state = {

        username: '',
        password: '',
        login:2,

    };
}

onButtonPress = () => {

          data.map((dataItem) =>{
            if(dataItem.user == this.state.username && dataItem.pass == this.state.password  ){

this.setState({ login: 1 }, () => {

    Alert.alert("Welcome");
    this.props.navigation.navigate("Home");



});

}

         }  )


         if (this.state.login = 2) {
             Alert.alert("Error");

         }



          }

Top comments (3)

Collapse
 
wolverineks profile image
Kevin Sullivan

Are you sending multiple user credentials to the app, and authenticating client-side?

Collapse
 
fedebais profile image
Federico Matias Bais

Hey Kevin thanks for you reply. I have a object with multiple users. Check this

const data = [{
id: '1',
user: 'admin',
pass: 'peluca1531'
}, {
id: '2',
user: 'admin2',
pass: 'peluca1531'
},
{
id: '3',
user: 'admin3',
pass: 'peluca1531'
},
{
id: '4',
user: 'admin4',
pass: 'peluca1531'
},

{
id: '5',
user: 'admin5',
pass: 'peluca1531'
},

{
id: '6',
user: 'admin6',
pass: 'peluca1531'
}, ];

Collapse
 
wolverineks profile image
Kevin Sullivan

id recommend not sending user credentials to the client, and instead send the user supplied credentials to the server, assuming there is a server part of this application.

that being said, you could try something like

codesandbox.io/s/quizzical-goodall...