Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Create a class function in react

Could someone complete the syntax of a function in a react class component that I want in order to press the onClick event and if the credentials are true will enter otherwise will give an error! I will post my thinking first:

Function with a const user={username: 'gerasimos', password: '27111980'} and then if(this.state.username & this.state.password ===user.password) { this.props.onRouteChange('home')

I have the idea in my head but not sure about the complete syntax.

My code is this:

  constructor (props) {
    super(props);
    this.state = {
      email: '',
      password: ''
    }
  }

  handleUserInput = (e) => {
    const name = e.target.name;
    const value = e.target.value;
    this.setState({[name]: value})}

  render () {
    return (
    <article className="br2 ba dark-gray b--black-30 mv6 w-100 w-50-m w-25-l mw5 center">
      <main className="pa4 black-80">
        <div className="measure center">
          <fieldset id="sign_up" className="ba b--transparent ph0 mh0">
            <legend className="f4 fw6 ph0 mh0">Log In</legend>
            <div className="mt3">
              <label className="db fw6 lh-copy f6" htmlFor="email-address">Email</label>
              <input className="pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100" 
                type="email" 
                name="email" 
                id="email-address"
                placeholder="Email"
                value={this.state.email}
                onChange={this.handleUserInput}  
              />
            </div>
            <div className="mv3">
              <label className="db fw6 lh-copy f6" htmlFor="password">Password</label>
              <input className="b pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100" 
                type="password" 
                name="password"  
                id="password"
                placeholder="Password"
                value={this.state.password}
                onChange={this.handleUserInput}
              />
            </div>
          </fieldset>
          <div className="">
            <input  onClick = {() => this.props.onRouteChange("home")}
              className="b ph3 pv2 input-reset ba b--black bg-transparent grow pointer f6 dib"
              type="submit" 
              value="Log in" 
            />
          </div>
        </div>
      </main>
    </article>
    );
  }
}```

Thank you in advance!
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

React documentation defines event handling beautifully. You should check this out.

In your code, you have email and password as input fields and not username and password.

<input
  onClick={this.handleOnclick}
  className="b ph3 pv2 input-reset ba b--black bg-transparent grow pointer f6 dib"
  type="submit"
  value="Log in"
/>

This is how you can define the onClick handler.

handleOnclick = () => {
  const user = { email: "gerasimos", password: "27111980" };
  if (
    this.state.email === user.email &&
    this.state.password === user.password
  ) {
    this.props.onRouteChange("home");
  }
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda