fix(order): stabilize zustand selector snapshots

Move filter-based derivations out of Zustand selectors in order/chat/review detail pages so snapshots stay stable under useSyncExternalStore checks. Add evidence-backed comments referencing React useSyncExternalStore guidance and Zustand issues #1936/#3155 to document the regression trigger.
This commit is contained in:
zetaloop
2026-02-22 09:05:04 +08:00
parent 2ade1780c1
commit acb04a02e7
3 changed files with 24 additions and 6 deletions
+7 -2
View File
@@ -2,7 +2,7 @@
import { ArrowLeft, Lock, Star } from "lucide-react"
import Link from "next/link"
import { use, useState } from "react"
import { use, useMemo, useState } from "react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Label } from "@/components/ui/label"
@@ -16,7 +16,12 @@ export default function ReviewPage({ params }: { params: Promise<{ id: string }>
const order = useOrderStore((state) => state.orders.find((item) => item.id === id))
const userId = useAuthStore((state) => state.user?.id)
const submitReview = useReviewStore((state) => state.submitReview)
const reviews = useReviewStore((state) => state.reviews.filter((item) => item.orderId === id))
const allReviews = useReviewStore((state) => state.reviews)
// The selector returns the raw store array and useMemo derives the subset.
// This keeps useSyncExternalStore snapshots stable across render checks.
// Inline filter inside the selector creates a new array reference each call
// and can cause infinite re-render loops in Zustand v5 (pmndrs/zustand#3155).
const reviews = useMemo(() => allReviews.filter((item) => item.orderId === id), [allReviews, id])
const [rating, setRating] = useState(0)
const [hoverRating, setHoverRating] = useState(0)
const [content, setContent] = useState("")