Using React/Redux, I have a simple react game where a user responds to a prompt from a number of choices, I want to log the response and whether it is correct. I have a 'GAME_BEGIN'
reducer like
case 'GAME_BEGIN':
return {
...state,
timeRemaining: action.timeRemaining,
gameIsInProgress: true,
problems: [
{
problem: action.problem, // string
solution: action.solution, // string
incorrectResponses: action.incorrectResponses, // array of strings
}
]
};
And now I want to add response
and isCorrect
attributes to the first problem object on action 'GAME_SUBMIT_RESPONSE'
, then add a new problem to the problems array. So after a few iterations (there can be many problems), I was thinking state should look like
...state,
problems: [
{
problem: something, // string
solution: something, // string
incorrectResponses: something, // array of strings
response: something,
isCorrect: somethingBool
},
...
{
problem: action.problem, // string
solution: action.solution, // string
incorrectResponses: action.incorrectResponses, // array of strings
}
]
For the first problem I might add attributes by just rewriting the entire problems
array, but how about once I add new objects?
Or am I thinking about this in the wrong way. I also thought I could flatten out the state and get rid of the problems
array altogether, resetting the problem on each submit and loggin the necessary data to the DB via api call. Is this the intended way of doing things?