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

88
Vistas
How to access the value of x after onclick is run

I have this code:

import React, { Component } from "react";

export default class Safe extends Component {
   constructor(props) {
      super(props);

      this.state = {
         userInput: "",
         password: 1234
      }
   }

   keyClicked = (evt) => {
      // HERE!!
   }

   render() {
      return (
         <div className="safe">
            <div className="display">{this.state.userInput}</div>
            <div className="keypad">
               {Array.from({length: 9}, (x, key) => <button className="key" key={key++} onClick={this.keyClicked}>{key++}</button>)}
            </div>
         </div>
      )
   }
}

I am attempting to make a "safe" with a number pad. In order to open the safe, the user must enter a specific code in order to "open it".

Currently, I am working on showing what the user has input through the number pad onto the display. However, I am unsure on how to get what number the user has inputted through the number pad. I have put a comment saying "HERE!!" to show where I want to access the inputted number.

I have attempted to use evt.target.value but when I try to console.log() the evt, it shows up as an empty string.

Any help is appreciated as I'm new to React!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Yea you can get this by this way

import React, { Component } from "react";

export default class Safe extends Component {
   constructor(props) {
      super(props);

      this.state = {
         userInput: "",
         password: 1234
      }
   }

   keyClicked = (evt) => {
     console.log(evt)
   }

   render() {
      return (
         <div className="safe">
            <div className="display">{this.state.userInput}</div>
            <div className="keypad">
               {Array.from({length: 9}, (x, key) => <button className="key" key={key++} onClick={()=>this.keyClicked(key++)}>{key++}</button>)}
            </div>
         </div>
      )
   }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think that for this case is better to use map(). And declare the array beforehand. This way it's possible to pass the number clicked as an argument to the click handler.

Try the following:

export default class Safe extends Component {
  constructor(props) {
    super(props);

    this.state = {
      userInput: "",
      password: 1234
    };
  }

  numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

  handleClick = (n) => {
    console.log(n)
  }

  render() {
    return (
      <div className="safe">
        <div className="display">{this.state.userInput}</div>
        <div className="keypad">
          {this.numbers.map((number, i) => {
            return (
              <button
                className="key"
                key={i}
                onClick={() => this.handleClick(number)}
              >
                {number}
              </button>
            )
          })}
        </div>
      </div>
    )
  }
}
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