Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bolt's Journal

## 2024-05-22 - [Ref Object Stability in Lists]
**Learning:** When rendering lists of items derived from a frequently updating global state (like a game state object), object references often change even if the data is identical. `React.memo`'s default shallow comparison fails here.
**Action:** Use a custom comparison function for `React.memo` that checks specific relevant fields of the data object, rather than relying on reference equality.
23 changes: 21 additions & 2 deletions frontend/src/components/game/PlayerList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, memo } from 'react';
import { Card, Tag, theme, Button, Modal } from 'antd';
import { getRoleNameWithEmoji } from '../../utils/roleUtils';
import { DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
Expand All @@ -13,7 +13,7 @@ interface PlayerCardProps {
onKick: () => void;
}

function PlayerCard({ player, isMe, canKick, onKick }: PlayerCardProps) {
function PlayerCardComponent({ player, isMe, canKick, onKick }: PlayerCardProps) {
const { token } = useToken();
const statusColor = player.is_online ? token.colorSuccess : token.colorError;

Expand Down Expand Up @@ -127,6 +127,25 @@ function PlayerCard({ player, isMe, canKick, onKick }: PlayerCardProps) {
);
}

function arePlayerCardPropsEqual(prev: PlayerCardProps, next: PlayerCardProps) {
// We explicitly ignore onKick because it's a new function on every render of PlayerList,
// but we know it's behaviorally stable (calls setKickTarget(player)).
// We compare relevant player fields to avoid re-renders when other parts of player object change (e.g. night actions).
return (
prev.isMe === next.isMe &&
prev.canKick === next.canKick &&
prev.player.id === next.player.id &&
prev.player.nickname === next.player.nickname &&
prev.player.is_online === next.player.is_online &&
prev.player.is_alive === next.player.is_alive &&
prev.player.is_spectator === next.player.is_spectator &&
prev.player.is_admin === next.player.is_admin &&
prev.player.role === next.player.role
);
}

const PlayerCard = memo(PlayerCardComponent, arePlayerCardPropsEqual);

interface PlayerListProps {
players: Player[];
myId: string | null;
Expand Down