Coding task · JavaScript and React course · Module 10: React Ecosystem and Future

Practice: Complex State with useReducer

useReducer(reducer, initialState) returns [state, dispatch]. A component sends an action, e.g. dispatch({ type: 'launch', id: 1 }), and React calls reducer(state, action) and renders the component with the new state. The reducer must be pure: instead of state.fuel -= ... it returns a new object ({ ...state, fuel: ... }), and it creates the changed mission with { ...m, status: ... } inside map, because React compares state by reference.

Task

Finish the mission control reducer. The launch action launches a planned mission if there is enough fuel. ___BLANK1___: the new fuel value is the fuel from state minus the fuelCost of the launched mission. ___BLANK2___: the launched mission becomes a new object with all the fields of the old one and the status 'in flight' (spread, without changing the old object). ___BLANK3___: App creates the state and dispatch with useReducer from missionReducer and initialState. The reducer must not mutate state: it always returns new objects and arrays.