usePlayerData Hook API Reference
File:packages/client/src/hooks/usePlayerData.ts
Added: March 2026 (PR #1067)
Overview
Centralized React hook for subscribing to player data events (inventory, equipment, stats, coins). Eliminates duplicate event listeners across components and provides proper equality checks to prevent cascading re-renders.Usage
Basic Usage with Context Provider
Direct Hook Usage (without context)
API
PlayerDataProvider
React context provider that wrapsusePlayerDataState and provides data to child components.
Props:
usePlayerDataContext
Hook to access player data from context. Must be used withinPlayerDataProvider.
Returns: PlayerDataState
Throws: Error if used outside PlayerDataProvider
Example:
usePlayerStatsContext
Hook to access only player stats from context. Must be used withinPlayerDataProvider.
Returns: PlayerStats | null
Example:
usePlayerDataState
Low-level hook that subscribes to player data events. UseusePlayerDataContext instead when possible.
Parameters:
PlayerDataState
Types
PlayerDataState
InventorySlotViewItem
PlayerEquipmentItems
PlayerStats
Event Handling
The hook subscribes to the following events:Equality Checks
The hook uses custom equality functions to prevent unnecessary re-renders:areInventoryItemsEqual
Compares two inventory arrays by slot, itemId, and quantity.true if inventories are equal, false otherwise
areEquipmentItemsEqual
Compares two equipment objects by item id, quantity, and name for each slot.true if equipment is equal, false otherwise
arePlayerStatsEqual
Compares two player stats objects including health, prayer, skills, and combat level.true if stats are equal, false otherwise
areSkillsEqual
Compares skill levels and XP for all skills.true if skills are equal, false otherwise
areStatusValuesEqual
Compares status values (health, prayer) by current and max.true if status values are equal, false otherwise
Initial Data Loading
The hook automatically requests initial data from the network cache on mount:- Check for player ID - Waits for
world.entities.player.idto be available - Load from cache - Reads
lastInventoryByPlayerId,lastSkillsByPlayerId,lastEquipmentByPlayerId,lastPrayerStateByPlayerId - Request fresh data - Emits
INVENTORY_REQUESTevent to server - Retry on failure - Retries after 400ms if player not ready
Performance Optimizations
Defensive Cloning
Inventory items are defensively cloned to prevent shared reference bugs:Equality Checks
All state updates use equality checks to prevent unnecessary re-renders:Merge Strategy
Player stats are merged instead of replaced to preserve data from different event sources:Type Guards
The hook uses type guards to validate event payloads:Cleanup
The hook properly cleans up all event listeners on unmount:Migration from Old Pattern
Before (Duplicate Subscriptions)
After (Centralized Subscription)
Benefits
- Eliminates Duplicate Listeners: Single subscription per event type across entire app
- Prevents Cascading Re-renders: Equality checks ensure components only re-render when data actually changes
- Type Safety: Type guards validate all event payloads
- Defensive Cloning: Prevents shared reference bugs
- Proper Cleanup: All listeners removed on unmount
- Cache Integration: Automatically loads from network cache on mount
- Player ID Filtering: Only processes events for local player (prevents cross-tab updates)
Related Hooks
- useModalPanels - Centralized modal panel state (bank, store, dialogue, etc.)
- useMinimapTerrainCache - Minimap terrain rendering
- useMinimapEntityPips - Minimap entity markers
- useMinimapWorldCaches - Minimap road/town caching
See Also
- UI Modernization Guide - Complete UI modernization details
- useModalPanels API - Modal panel hook reference
- Minimap Hooks API - Minimap hook reference