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