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

348
Vistas
React onClick function variable undefined, but should work during runtime, how should I fix this?

I have an onclick function called Selection and I call it in the functional component.

I have multiple buttons but I want only one of them to be selected at the same time, thus written such a function and all of the button share the same onclick function Selection().

However, this return's variable undefined error due to that boardSetHooks access of the last_select variable. If I understand my code correctly, when that else statement is executed, no such case that last_selected[0] will be undefined since it will be caught by the first if statement.

How should I solve this? I am doing some research and I'm not sure which solution or direction I should look into: using useEffect() or using bind?

var last_select = []

export default function Grid() {

    function Selection(x, y){
        if (last_select === []) {
            if (board[x][y][0] !== "" && board[x][y][0][0] === 'W'){
                boardSetHooks[x][y]([board[x][y][0], true])
                last_select = [x, y]
                return
            }
        }
        else {
            if (last_select === [x, y]) {
                boardSetHooks[x][y]([board[x][y][0], false])
                last_select = []
                return
            }
            else {
                boardSetHooks[last_select[0]][last_select[1]]([board[last_select[0]][last_select[1]][0], false])
                boardSetHooks[x][y]([board[x][y][0], true])
                last_select = [x, y]
                return
            }
        }
    }


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

0

  1. Each of the "return" statements do not actually return anything. What should they be returning? If you want to ALWAYS return last_select, then:
    1. Remove all your returns
    2. At the END of Selection, return last_select;
  2. After formatting your code a bit, you can see there may be a situation where your if-else statement does not cover all possibilities.
function Selection(x, y) {
    if (last_select === []) {
        if (board[x][y][0] !== "" && board[x][y][0][0] === 'W'){
            boardSetHooks[x][y]([board[x][y][0], true])
            last_select = [x, y]
            return // This returns undefined. What should it return?
        }

        // <----- What happens when the above if-condition is false?

    } else if (last_select === [x, y]) {
        boardSetHooks[x][y]([board[x][y][0], false])
        last_select = []
        return // This returns undefined. What should it return?
    } else {
        boardSetHooks[last_select[0]][last_select[1]]([board[last_select[0]][last_select[1]][0], false])
        boardSetHooks[x][y]([board[x][y][0], true])
        last_select = [x, y]
        return // This returns undefined. What should it return?
    }
    return last_select // <--- All of your if-statements assign last_select, so return it here.
}
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