Add stable viewKey API to prevent UI re-renders during ID transitions #734
OpenKyleAMathewswants to merge 11 commits into
main fromclaude/review-issue-19-011CUdcC6qxdLp2AeFvUAuWdopened kevin-dp(GitHub profile, opens in a new tab) approved these changes
@@ -450,3 +450,21 @@450450 return this.config.getKey(item)451451 }452452453+ /**454+ * Get a stable view key for a given item key.455+ * If viewKey configuration is enabled, returns the stable viewKey.456+ * Otherwise, returns the key as a string (backward compatible behavior).457+ *458+ * @param key - The item key to get the view key for459+ * @returns The stable view key as a string460+ *461+ * @example462+ * // Use in React components for stable keys during ID transitions463+ * {todos.map((todo) => (464+ * <li key={collection.getViewKey(todo.id)}>465+ * {todo.text}466+ * </li>467+ * ))}468+ */469+ public getViewKey(key: TKey): string {470+ const viewKey = this._state.viewKeyMap.get(key)@@ -451,2 +451,21 @@451451 }452452453+ /**454+ * Get a stable view key for a given item key.455+ * If viewKey configuration is enabled, returns the stable viewKey.456+ * Otherwise, returns the key as a string (backward compatible behavior).457+ *458+ * @param key - The item key to get the view key for459+ * @returns The stable view key as a string460+ *461+ * @example462+ * // Use in React components for stable keys during ID transitions463+ * {todos.map((todo) => (464+ * <li key={collection.getViewKey(todo.id)}>465+ * {todo.text}466+ * </li>467+ * ))}468+ */469+ public getViewKey(key: TKey): string {470+ const viewKey = this._state.viewKeyMap.get(key)471+ return viewKey ?? String(key)@@ -452,0 +476,21 @@476+ * This is used when server-generated IDs replace temporary client IDs.477+ *478+ * @param tempKey - The temporary key used during optimistic insert479+ * @param realKey - The real key assigned by the server480+ *481+ * @example482+ * // In your insert handler, link temp ID to real ID483+ * onInsert: async ({ transaction }) => {484+ * const mutation = transaction.mutations[0]485+ * const tempId = mutation.modified.id486+ *487+ * const response = await api.todos.create(mutation.modified)488+ * const realId = response.id489+ *490+ * // Link the IDs so they share the same viewKey491+ * collection.mapViewKey(tempId, realId)492+ *493+ * await collection.utils.refetch()494+ * }495+ */496+ public mapViewKey(tempKey: TKey, realKey: TKey): void {@@ -50,3 +50,5 @@5050 public optimisticUpserts = new Map<TKey, TOutput>()5151 public optimisticDeletes = new Set<TKey>()525253+ // ViewKey mapping for stable rendering keys across ID transitions54+ public viewKeyMap = new Map<TKey, string>()@@ -176,3 +176,9 @@176176 }177177 const globalKey = this.generateGlobalKey(key, item)178178179+ // Generate viewKey if configured180+ let viewKey: string | undefined181+ if (this.config.viewKey) {182+ viewKey = this.config.viewKey(validatedData)183+ // Store viewKey mapping184+ this.state.viewKeyMap.set(key, viewKey)samwillis reviewed
@@ -50,3 +50,5 @@5050 public optimisticUpserts = new Map<TKey, TOutput>()5151 public optimisticDeletes = new Set<TKey>()525253+ // ViewKey mapping for stable rendering keys across ID transitions54+ public viewKeyMap = new Map<TKey, string>()KyleAMathews linked this to fix #19
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 518c98313fed0f050860b9275956d367454870d7
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR