Skip to content

Commit 0f06c30

Browse files
committed
chore: prevent MemoizedDrawer remount when filterOptions reference changes
Use a ref to hold the latest filterOptions in useListDrawer so that updates to filterOptions (e.g. after selecting a value) do not recreate MemoizedDrawer. A new function reference causes React to unmount and remount ListDrawerContent, resetting selectedOption state — which broke the polymorphic relationship list drawer filter.
1 parent c17e96e commit 0f06c30

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

packages/ui/src/elements/ListDrawer/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22
import { useModal } from '@faceless-ui/modal'
3-
import React, { useCallback, useEffect, useId, useMemo, useState } from 'react'
3+
import React, { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
44

55
import type { ListDrawerProps, ListTogglerProps, UseListDrawer } from './types.js'
66

@@ -119,20 +119,26 @@ export const useListDrawer: UseListDrawer = ({
119119
openModal(drawerSlug)
120120
}, [drawerSlug, openModal])
121121

122+
// Use a ref so that filterOptions updates don't recreate MemoizedDrawer.
123+
// A new MemoizedDrawer reference causes React to unmount/remount ListDrawerContent,
124+
// which resets its internal state (e.g. selectedOption) — breaking polymorphic drawers.
125+
const filterOptionsRef = useRef(filterOptions)
126+
filterOptionsRef.current = filterOptions
127+
122128
const MemoizedDrawer = useMemo(() => {
123129
return (props) => (
124130
<ListDrawer
125131
{...props}
126132
closeDrawer={closeDrawer}
127133
collectionSlugs={collectionSlugs}
128134
drawerSlug={drawerSlug}
129-
filterOptions={filterOptions}
135+
filterOptions={filterOptionsRef.current}
130136
key={drawerSlug}
131137
selectedCollection={selectedCollection}
132138
uploads={uploads}
133139
/>
134140
)
135-
}, [drawerSlug, collectionSlugs, uploads, closeDrawer, selectedCollection, filterOptions])
141+
}, [drawerSlug, collectionSlugs, uploads, closeDrawer, selectedCollection])
136142

137143
const MemoizedDrawerToggler = useMemo(() => {
138144
return (props) => <ListDrawerToggler {...props} drawerSlug={drawerSlug} />

0 commit comments

Comments
 (0)