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

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

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 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