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

159
Views
How can I get rid of so many dispatches?

In my mapDispatchToProps I call draw which draws on my square when pressed, cross or zero. Now the code looks like this:

const mapStateToProps = ({board, players}) => ({board, players});
const mapDispatchToProps =  dispatch => ({
    draw:  (board, players, squareIndex) => {
        if (!board[squareIndex]) {
            if (players[players.turn] === 'X') {
                  dispatch(drawX(squareIndex));
            } else {
                  dispatch(drawO(squareIndex));
            }

            dispatch(checkResult());
            dispatch(toggleTurn());
        }
    }
});

I would like to do something like:

const mapDispatchToProps =  dispatch => ({
    draw
});

it`s possible? Redux-thunk can help me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should probably only dispatch one action with all relevant infomation here and handle that in mulitple reducers. Even the decision if an X or an O should be drawn should happen in your Reducer, not in your Component.

Just have a case for the same action in multiple reducers. every action will always be forwarded to every reducers.

If you are using Redux Toolkit (which nowadays you really should), you can use extraReducers for that.

Relevant conecptual reading from the Redux Style Guide:

https://redux.js.org/style-guide/style-guide/#model-actions-as-events-not-setters
https://redux.js.org/style-guide/style-guide/#allow-many-reducers-to-respond-to-the-same-action
https://redux.js.org/style-guide/style-guide/#avoid-dispatching-many-actions-sequentially

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!