import { NAME, pattern, UI, type VNode } from "commonfabric"; // ===== Types ===== type ReproInput = Record; export interface ReproOutput { [NAME]: string; [UI]: VNode; } // ===== Shared style object (BUG) ===== // This single object reference is reused across all siblings. // The runtime sees the same reference and assumes nothing changed, // so only the first element actually gets the style applied. const sharedCardStyle = { background: "white", borderRadius: "8px", boxShadow: "0 1px 3px rgba(0,0,0,0.12)", padding: "16px", borderLeft: "4px solid #3b82f6", marginBottom: "8px", }; // ===== Factory function (FIX) ===== // Returns a fresh object each time, so the runtime sees a new // reference for every sibling and applies the style correctly. function makeCardStyle() { return { background: "white", borderRadius: "8px", boxShadow: "0 1px 3px rgba(0,0,0,0.12)", padding: "16px", borderLeft: "4px solid #10b981", marginBottom: "8px", }; } // ===== Pattern ===== const StyleObjectRepro = pattern(() => { const bugItems = ["Card A", "Card B", "Card C", "Card D", "Card E"]; const fixItems = ["Card A", "Card B", "Card C", "Card D", "Card E"]; return { [NAME]: "Style Object Reference Gotcha", [UI]: ( Style Object Reference Gotcha {/* ===== BUG DEMO ===== */} Bug Demo (shared style object)
These boxes share a style object reference — only the first one renders correctly:
{bugItems.map((label) => (
{label} — using sharedCardStyle
))}
{/* ===== Divider ===== */}
{/* ===== FIX DEMO ===== */} Fix Demo (factory function)
These boxes each get a fresh style object from a factory function — all render correctly:
{fixItems.map((label) => (
{label} — using makeCardStyle()
))}
), }; }); export default StyleObjectRepro;