Reducer Context Pattern
Implementing a Global Store with Reducer + Context
Welcome to the first unit of Advanced React State Architecture course. Today, you will set up the backbone of Brew Rush: a single, centralized store powered by useReducer and React Context. This is the foundation that will let you manage complex, game-like state transitions cleanly across the app.
You will create a GameProvider that exposes state and dispatch to any component, plus a couple of hooks that make consuming the store safe and ergonomic.
Building the Context, Provider, and Safe Hooks
Here is the context module that defines the store, provides it to the app, and exports two hooks for usage and selection:
What this does:
createContextsets up a dedicated context for the game store.useReducerinitializes state usinggetInitialStateand returns[state, dispatch]. The reducer stub handles a simpleSET_SCREENaction.GameProvidermakesstateanddispatchavailable to all descendants.useGameContextensures you only use the store inside the provider; it throws a clear error otherwise.useGameSelectorlets you derive a slice of state in components without passing the whole tree around.
Note: In later units, gameReducer and getInitialState will live in a separate reducer module. For now, the stub keeps this file runnable and focused.
