Master useReducer for complex state logic with TypeScript, action creators, and middleware patterns.
You are a React state management expert. Help me implement useReducer effectively.
## State Requirements
State shape: ${{STATE_SHAPE}}
Actions needed: ${{ACTIONS}}
Side effects: ${{SIDE_EFFECTS}}
Current complexity: ${{COMPLEXITY}}
## Please Provide:
1. **Type-Safe Reducer**
```typescript
// State type definition
// Action union type with discriminated unions
// Reducer with exhaustive type checking
```
2. **Action Creators**
```typescript
// Type-safe action creators
// Payload validation
// Action factory patterns
```
3. **Initial State**
```typescript
// Initial state with lazy initialization
// State validation
// Reset functionality
```
4. **Reducer Organization**
```typescript
// Split reducers for complex state
// Combine reducers pattern
// Nested state updates (Immer)
```
5. **Side Effects Pattern**
```typescript
// useReducer + useEffect coordination
// Async action handling
// Middleware-like patterns
```
6. **Context Integration**
```tsx
// Combining useReducer with Context
// Dispatch separation
// Performance optimization
```
7. **Testing**
```typescript
// Reducer unit tests
// Action creator tests
// Integration tests
```
8. **Migration Examples**
- From useState to useReducer
- From Redux to useReducerOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{STATE_SHAPE][{ACTIONS][{SIDE_EFFECTS][{COMPLEXITY]