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

244
Vistas
delay on getting property, dispatch react-redux

Delay when updating the board component, my dispatch (checkResult (board)); does not work correctly. Tic-tac-toe game, here's an example of a problem: set 3 crosses but no victory

set 3 crosses but no victory, but when I do 1 more action (set a cross or zero), then the victory is counted:

My code in:

 const mapStateToProps = ({board, players}) => ({board, players});

const mapDispatchToProps =  dispatch => ({
    draw:  (board, players, squareIndex) => {
        if (!board[squareIndex]) {
            if (players[players.turn] === 'X') {
                  dispatch(drawXAction(squareIndex));
            } else {
                  dispatch(drawOAction(squareIndex));
            }
            console.log(dispatch(checkResult(board)))

            dispatch(checkResult(board));
            dispatch(toggleTurnAction());
        }
    }
});

export default connect(mapStateToProps, mapDispatchToProps)(Square); 

And checkResult func:

export function checkResult(board) {
    if (checkVictory(board, 'X')) {
        return {
            type: X_WINS
        }
    } else if (checkVictory(board, '0')) {
        return {
            type: O_WINS
        }
    } else {
        const check = board.filter(symbol=>symbol===null);
        if(check.length===1) {
            return {
                type: TIE
            }
        }else {
            return {
                type: 'RANDOM'
            }
        }
    }
}

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

0

You are passing the current board to the checkResult function, this means when your checkResult function is executing, it is not receiving the latest board -- what you have updated in the one of the previous lines.

One of the redux principle states that - it enable single source of truth. Your code is violating this principle resulting in this inconsistency. What you need to do is - get the latest state of the application in checkResult function rather than passing the board as argument.

e.g.

import store from "/path/to/store";

export function checkResult() {

  // or something like this
  // based on what you have in your store.
  const board = store.getState().board;

  // your function body
}
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