Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda