@@ -4,7 +4,7 @@ import ListItemButton from "@mui/material/ListItemButton";
44import Paper from "@mui/material/Paper" ;
55import type { MentionNodeAttrs } from "@tiptap/extension-mention" ;
66import type { SuggestionOptions , SuggestionProps } from "@tiptap/suggestion" ;
7- import { forwardRef , useEffect , useImperativeHandle , useState } from "react" ;
7+ import { forwardRef , useImperativeHandle , useState } from "react" ;
88import type { MentionSuggestion } from "./mentionSuggestionOptions" ;
99
1010export type SuggestionListRef = {
@@ -24,6 +24,14 @@ const SuggestionList = forwardRef<SuggestionListRef, SuggestionListProps>(
2424 ( props , ref ) => {
2525 const [ selectedIndex , setSelectedIndex ] = useState ( 0 ) ;
2626
27+ // Follow React's recommendation to avoid calling setState within an effect
28+ // https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
29+ const [ prevItems , setPrevItems ] = useState ( props . items ) ;
30+ if ( props . items !== prevItems ) {
31+ setPrevItems ( props . items ) ;
32+ setSelectedIndex ( 0 ) ;
33+ }
34+
2735 const selectItem = ( index : number ) => {
2836 if ( index >= props . items . length ) {
2937 // Make sure we actually have enough items to select the given index. For
@@ -62,10 +70,6 @@ const SuggestionList = forwardRef<SuggestionListRef, SuggestionListProps>(
6270 selectItem ( selectedIndex ) ;
6371 } ;
6472
65- useEffect ( ( ) => {
66- setSelectedIndex ( 0 ) ;
67- } , [ props . items ] ) ;
68-
6973 useImperativeHandle ( ref , ( ) => ( {
7074 onKeyDown : ( { event } ) => {
7175 if ( event . key === "ArrowUp" ) {
0 commit comments