Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!