Skip to main content

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 wraps usePlayerDataState and provides data to child components. Props:
Example:

usePlayerDataContext

Hook to access player data from context. Must be used within PlayerDataProvider. Returns: PlayerDataState Throws: Error if used outside PlayerDataProvider Example:

usePlayerStatsContext

Hook to access only player stats from context. Must be used within PlayerDataProvider. Returns: PlayerStats | null Example:

usePlayerDataState

Low-level hook that subscribes to player data events. Use usePlayerDataContext instead when possible. Parameters:
Returns: 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.
Returns: true if inventories are equal, false otherwise

areEquipmentItemsEqual

Compares two equipment objects by item id, quantity, and name for each slot.
Returns: true if equipment is equal, false otherwise

arePlayerStatsEqual

Compares two player stats objects including health, prayer, skills, and combat level.
Returns: true if stats are equal, false otherwise

areSkillsEqual

Compares skill levels and XP for all skills.
Returns: true if skills are equal, false otherwise

areStatusValuesEqual

Compares status values (health, prayer) by current and max.
Returns: true if status values are equal, false otherwise

Initial Data Loading

The hook automatically requests initial data from the network cache on mount:
  1. Check for player ID - Waits for world.entities.player.id to be available
  2. Load from cache - Reads lastInventoryByPlayerId, lastSkillsByPlayerId, lastEquipmentByPlayerId, lastPrayerStateByPlayerId
  3. Request fresh data - Emits INVENTORY_REQUEST event to server
  4. 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:
Example:

Cleanup

The hook properly cleans up all event listeners on unmount:

Migration from Old Pattern

Before (Duplicate Subscriptions)

After (Centralized Subscription)

Benefits

  1. Eliminates Duplicate Listeners: Single subscription per event type across entire app
  2. Prevents Cascading Re-renders: Equality checks ensure components only re-render when data actually changes
  3. Type Safety: Type guards validate all event payloads
  4. Defensive Cloning: Prevents shared reference bugs
  5. Proper Cleanup: All listeners removed on unmount
  6. Cache Integration: Automatically loads from network cache on mount
  7. Player ID Filtering: Only processes events for local player (prevents cross-tab updates)
  • useModalPanels - Centralized modal panel state (bank, store, dialogue, etc.)
  • useMinimapTerrainCache - Minimap terrain rendering
  • useMinimapEntityPips - Minimap entity markers
  • useMinimapWorldCaches - Minimap road/town caching

See Also