It doesn’t make much sense what you have there as we’re lacking the context.
You are making a copy of the state for no reason afaics. You are returning a new array with two elements, you don’t use any of the values that are currently in the state so there’s no point referencing it.
The reducer is a function that looks like this:
(state: State, action: Action) => State
So it takes a data structure of a specific shape (State) and an action (Action, which is an object with a type
field + possibly some other fields) and returns a data structure of the same shape as State.
NOTE I am writing the type of things these are, just treat this as pseudocode, it’s to try to make it clear what is going into the function and what is coming out
As I say there is no context here, but I assume you are doing the pomodoro timer?
If you are returning your state as that array of two items, that’s your state. An array with a time and whatever session is. I don’t think this is what you meant to do as it’s very difficult to deal with (an object is what it should be, like { break: 300, session: "whatever this is" }
, but anyway).
When you decrement, you return an array with the break and session, exactly the same types, but in that case the number for the break would be decremented.
const initialState = [defaultBreak, defaultSession];
function timer (state = initialState, action) {
switch (action.type) {
case "DECREMENT":
return [state[0] - 1, state[1]];
case etc.....
Do you see that it’s just a function that takes what the current values of the state are (ie the things you’re interested in that get changed by user input) then, depending on what message is sent, returns a new version of that state with adjusted values.