Zadanie z kodem · Kurs JavaScript i React · Moduł 4: Context API i useReducer
Praktyka: useReducer podstawy
useReducer dla złożonego stanu: const [state, dispatch] = useReducer(reducer, initialState). Reducer: (state, action) => switch(action.type) { case 'INCREMENT': return {...state, count: state.count + 1} }. Dispatch: dispatch({ type: 'INCREMENT' }).
Zadanie
Stwórz reducer dla energii: initialState = { engines: 50, shields: 30, weapons: 20 }. Akcje: BOOST_ENGINES (+10), BOOST_SHIELDS (+10), BOOST_WEAPONS (+10). dispatch({ type: 'BOOST_ENGINES' }). Wyświetl wszystkie wartości!