Issue with Material UI/React - unable to display helper text

My login react component is not showing the 'wrong credentials' error in the helper text even when false credentials are entered. I am pushing an error message into an array, assigning it to state, and then referencing that state in my helperText prop. Here's the code snippet from my login component:

import React from 'react'
import Header from './Header'
import TextField from "@material-ui/core/TextField";
import Button from '@material-ui/core/Button'
import axios from 'axios'
import {Link} from 'react-router-dom'
import firebase from 'firebase'
import {auth} from 'firebase/app'


class Login extends React.Component {
  constructor() {
    super();
    this.state = {
      email: "",
      password: "",
      errors:[]
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }



  handleChange(e) {
    this.setState({
      [e.target.name]: e.target.value,
    });
  }

  handleSubmit(e) {
    e.preventDefault()
    const { email, password } = this.state
    const { history } = this.props
    const errorLog = []
    this.setState({
      errors: errorLog
    })
    auth().signInWithEmailAndPassword(email, password)
      .then((res) => {
        console.log(res)
        history.push('/gigregister')
      })
      .catch((err) => {
        if(err.code = 'auth/wrong-password'){
         errorLog.push('Wrong credentials, please try again')
         console.log(`this is error state ${this.state.errors}`)
        }
      })
  }

  render() {
  
    return (
      <>
        <div>
          <Header />
        </div>
        <div className ='gig-button'>
        <Link to="/Homepage" style={{ textDecoration: "none" }}>
         <Button>Gigs this week</Button>
        </Link>
        </div>
        <div className="login-main">
          <div className="login">
            <h4>Venue login</h4>
            <form onSubmit={this.handleSubmit}>
            <TextField
                placeholder="Enter email"
                id="email"
                name="email"
                onChange={this.handleChange}
              />
              <TextField
                placeholder="Enter password"
                id="password"
                name="password"
                error ={this.state.errors ? true : false}
                helperText = {this.state.errors}
                onChange={this.handleChange}
              />
              <div className="button">
                <Button type="submit">Submit</Button>
              </div>
            </form>
          </div>
          <Link to="/venueregister" style={{ textDecoration: "none" }}>
            <Button>Register a venue</Button>
          </Link>
        </div>
      </>
    );
  }
}

export default Login;

Answer №1

There seems to be an issue with your condition statement

if(err.code = 'auth/wrong-password'){

I believe you intended to compare these values using == or ===.

As for the errors not appearing

Currently, I am adding an error message to an array and then assigning that error to the state

Make sure to update the state after pushing to the errorLog array - remember to set the state after pushing.

.catch((err) => {
  if(err.code === 'auth/wrong-password'){
    errorLog.push('Incorrect credentials, please try again')
    this.setState({
      errors: errorLog
    });
  }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message in Leaflet JS: Unable to access property 'addLayer' because it is undefined when trying to display drawnItems on the Map

My attempt to showcase a Leaflet Map with polygon-shaped surfaces encountered an issue. Sometimes, I face the error "cannot read property 'addLayer' of undefined," but when the page is refreshed, the map renders correctly. I am unsure where I wen ...

I need help figuring out how to set the country code as uneditable and also display a placeholder in react-phone-input-2 with React

How can I display the placeholder after the country code without allowing it to be edited? Currently, it's not showing up in my React functional components. Here is the code snippet: <PhoneInput className="inputTextDirLeft" ...

Is there a way to display my array within my React component using JavaScript?

I am working with an element that looks like this: const DropdownElements = [ { key: 1, title: "City", placeholder: "Select City", apiUrl: "https://api.npoint.io/995de746afde6410e3bd", type: "city&qu ...

Unable to preserve the value of react material-ui autoCompletion using formik

Greetings everyone! Today, I wanted to address an issue that has been causing some trouble for me recently. I've been utilizing the autoCompletion feature from materiall-ui along with formik in reactjs. However, I've been facing difficulties when ...

Using the combination of Nextjs, rsuitejs, and CSS modules ensures that no CSS files will be

In my development work, I have been utilizing Next.js in conjunction with RSuiteJS. During the development phase, everything is working smoothly without any issues. However, after building the project, I have encountered a problem where the library CSS fi ...

Creating Javascript objects using provided JSON data

Good day! I am looking for assistance in understanding how to create objects from JSON data that has been provided: { "type":"FeatureCollection", "features":[ { "type":"Feature", ...

Tips for successfully sending an API request using tRPC and NextJS without encountering an error related to invalid hook calls

I am encountering an issue while attempting to send user input data to my tRPC API. Every time I try to send my query, I receive an error stating that React Hooks can only be used inside a function component. It seems that I cannot call tRPC's useQuer ...

Style for Selected Avatar Chips in Flutter FilterChip

I'm using a `FilterChip` along with a `CircleAvatar` for the avatar, but when it's selected, there's an unwanted rectangle that I can't seem to locate any information about. How can I get rid of it? Here is the code snippet: child: Fi ...

Locating the DataGrid Component in Material-UI v5: A Guide on Installing DataGrid in React

After updating material-ui/core to version 5.0.0-beta.1 and material-ui/lab to version 5.0.0-alpha.30, I am unable to find the DataGrid component. It does not seem to be included in either material-ui/core or material-ui/lab. Additionally, the documentat ...

Running Jest encounters errors when there is ES6 syntax present in the node modules of a create-react-app project

Currently, I am working on a project using create-react-app and attempting to perform unit testing on a component from office-ui-fabric-react using Jest and Enzyme. The most recent version of office-ui-fabric-react utilizes es6 syntax which is causing iss ...

The order of CSS precedence shifts even when the class sequence is the same

In my development setup, I have a react component that is embedded within two distinct applications each with their own webpack configurations. Within the component, I am utilizing a FontAwesome icon using the react-fontawesome library: <FontAwesom ...

Guide on retrieving the state within a function

this.state = { color: { B: '#FFB1C2', // RED C: '#A7EBEB', // GREEN D: '#99BFFF', // BLUE E: '#F9C499', // ORANGE F: '#B2A9A7&apo ...

Using the React UseEffect Hook allows for value updates to occur within the hook itself, but not within the main

I am currently utilizing a font-picker-react package to display fonts using the Google Font API. Whenever a new font is chosen from the dropdown, my goal is to update a field value accordingly. While the 'value' updates correctly within the ...

How to integrate SCSS into Next.js for enhanced styling capabilities?

editing the issue : __app instead of _app. I'm feeling a bit lost with my current problem. I'm trying to implement SCSS in my React/Next.js application but nothing seems to be working. I have installed the following packages: yarn add node-sas ...

Exploring the potential of Reactjs and Material UI with a nested sidebar menu

I'm having trouble creating a nested sidebar menu using Material UI. While I can easily make a simple list, my project requires a nested menu that I can't seem to achieve. When attempting to use a recursive function, it only shows the main title ...

Inquiring about the compatibility of styled-components with material-ui

Greetings! I am diving into the world of React and exploring styling for the first time. As a newcomer, I am attempting to style the material-ui Button component with styled components. My approach involves overriding global class names, such as MuiButton ...

Is there a method to retrieve Mui state classes easily?

One thing I really appreciate is the way to style mui-components with their class names. I'm curious if there's a method to access state classes like Mui-checked using a variable. Let me delve deeper into this: I have a styled component that lo ...

Implementing Isotope layout in Reactjs with data-attributes for filtering and sorting content

I am currently working on optimizing the isotope handler within a react component. My goal is to implement filter options such as category[metal] or category[transition], as well as combo filters like category[metal, transition]. Here is the sandbox for t ...

Conceal the Checkbox Column in MUI X DataGrid when generating a printout

Is there a solution to hide the checkbox column in my MUI X DataGrid only when the table is being printed by the user? I have attempted to hide the checkbox, but it also disappeared from the table itself. Here's a screenshot showing the table in prin ...

The npm installation failed to execute when using Node.js version 16.0.0

Hey, I just cloned my repository and attempted to run npm install but encountered an error npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! Found: <a href="/cdn-cgi/l/email-protection" class= ...