API ReferenceReact HooksusePlayersState()

usePlayersState

The return value is an array of objects with the following shape: - player: PlayerState - state: any

Signature

function usePlayersState<T = any>(key: string): { player: PlayerState; state: T; }[]

Parameters

NameTypeDescription
keystringThe key of the player state to get for all players.

Returns

{ player: PlayerState; state: T; }[]

Usage

import { usePlayersState } from 'playroomkit'
 
function Leaderboard() {
  const players = usePlayersState('score');
  // players = [{player: PlayerState, state: 10}, {player: PlayerState, state: 20}]
 
  const sortedPlayers = players.sort((a, b) => b.state - a.state);
 
  return (
    <div>
      <h3>Leaderboard</h3>
      {sortedPlayers.map(({ player, state: score }, index) => (
        <div key={player.id}>
          {index + 1}. {player.getProfile().name}: {score} points
        </div>
      ))}
    </div>
  );
}