I'm struggling to understand Redux and am loosing a local state upon hitting a button on a React page. The situation is as follows:
Suppose you are on a React page, which has a local state:
{"player": "PersonA"}
There's also a button on the same React page. Pushing the button triggers a Redux action. The associated controller method returns:
return res.status(200).json({
statusCode: 200,
success: true,
payment: {"amount": "3", "currency": "EUR"},
});
And the reducer:
case APPLY_SUCCESS:
console.log(...state); // returns undefined
return { ...state, paymentData: action.payload.payment };
After hitting the button on the React page, the local state only contains paymentData. So player is gone from the local state. I thought ...state in the reducer should ensure maintaining the existing local state that is unaffected by the action. So it would just append paymentData to the local state and also keep player.
My question:
- Am I correct that
...statein the reducer is supposed to ensure that the local state that is unaffected by the action, remains part of the local state? - If so, what could be the reason
...stateis undefined? How can I ensureplayeris still part of the local state?
Comments
Post a Comment