0

I'm trying to make a login page and I want to display an error if the login fails. I call the function here: onClick={ this.login } But I want the function to display the error message without re-rendering everything. The function is inside a class

1

2 Answers 2

0

Try something like this:


const [username, setUsername] = useState('');
const [error, setError] = useState('');

const handleLogin = async () => {
  // request a login to your server
  const res = await fetch('http://yourapi.com/login');
  // determine if the request was successful
  if (res.error) {
    setError('Invalid input.')
  }
  else {
    // continue
  }
};

return (
  <Form onSubmit={handleLogin}>
     <span>{error}</span>
     <input onChangeText={text => setUsername(text)}/>
  </Form>
);

Sign up to request clarification or add additional context in comments.

Comments

0

Found out how to do it by creating an invisible alert that's visible when I set a variable to the error

         {
          this.state.status && 
          <Alert severity="error">
            <AlertTitle>Error</AlertTitle>
            { this.state.status }
          </Alert>
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.