Tengo una función onclick llamada Selección y la llamo en el componente funcional.
Tengo varios botones, pero solo quiero que se seleccione uno de ellos al mismo tiempo, por lo tanto, se escribe una función de este tipo y todos los botones comparten la misma función de clic Selection() .
Sin embargo, el error indefinido de la variable de retorno se debe al acceso de boardSetHooks a la variable last_select. Si entiendo mi código correctamente, cuando se ejecuta esa declaración else, no habrá tal caso de que last_selected[0] no esté definido, ya que será capturado por la primera declaración if.
¿Cómo debo resolver esto? Estoy investigando un poco y no estoy seguro de qué solución o dirección debo buscar: ¿usar useEffect() o usar 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 } } }last_select , entonces:Selection , return last_select; 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. }