Receive the value of an input variable and compare to a preset to generate an Alert and redirect

Asked

Viewed 17 times

-1

Good morning, I’m new to React Native and I’m trying to compare the value of a variable I received through a Textinput with an already preset to generate an Alert and redirect me to another screen, only I can’t make the comparisons work and let alone the variable receive the value to jump screen or not, could give me a strength?

I took the Return to resume the value, but I can’t compare the login-login1 and password-pass1 variables on it.

import React, {useState} from 'react';
import { Sucess } from './src/screens/Sucess';
import { Denied } from './src/screens/Denied';

export default function App(){

  const [login1,setlogin1] = useState('');
  const [senha1,setsenha1] = useState('');

  const ConfereSenha = () => {
    let login = "test";
    let senha = "1234";

    return(
      if(login1 != login && senha1 != senha){
        alert("Senha incorreta")
        <Denied />
      } else {
        alert("Acesso liberado")
        <Sucess />
      })
  }

<TextInput secureTextEntry={true} placeholder='Login' onChangeText={(login1)=>setlogin1(login1)} maxLength={10}></TextInput>

<TextInput secureTextEntry={true} onChangeText={(senha1)=>setsenha1(senha1)} maxLength={10}

<TouchableOpacity style={styles.botaolog} onPress={ConfereSenha}>ENTRAR</TouchableOpacity>

1 answer

0

Good morning Eder, all right? No re-act the expression:

return(
  if(login1 != login && senha1 != senha){
    alert("Senha incorreta")
    <Denied />
  } else {
    alert("Acesso liberado")
    <Sucess />
  }
)

It’s incorrect. I believe you’re using JSX, so you could do

if(login1 != login && senha1 != senha){
  alert("Senha incorreta")
} else {
  alert("Acesso liberado")
}

return(
  {login1 != login && senha1 != senha ? <Denied /> : <Sucess />}
)

These 3 elements must be within the return of the App component’s yield as well.


<TextInput secureTextEntry={true} placeholder='Login' onChangeText={(login1)=>setlogin1(login1)} maxLength={10}></TextInput>

<TextInput secureTextEntry={true} onChangeText={(senha1)=>setsenha1(senha1)} maxLength={10}

<TouchableOpacity style={styles.botaolog} onPress={ConfereSenha}>ENTRAR</TouchableOpacity>

However, even with the fixes, it won’t work the way you expect. Your code has a set of other errors. I would advise you to look at the documentation of React so that you could fix the logic and JSX errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.