Skip to content

Commit 45bd958

Browse files
committed
Fix new react hook eslint violations
1 parent 7521738 commit 45bd958

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/demo/SuggestionList.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ListItemButton from "@mui/material/ListItemButton";
44
import Paper from "@mui/material/Paper";
55
import type { MentionNodeAttrs } from "@tiptap/extension-mention";
66
import type { SuggestionOptions, SuggestionProps } from "@tiptap/suggestion";
7-
import { forwardRef, useEffect, useImperativeHandle, useState } from "react";
7+
import { forwardRef, useImperativeHandle, useState } from "react";
88
import type { MentionSuggestion } from "./mentionSuggestionOptions";
99

1010
export 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") {

src/extensions/ResizableImageComponent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ function ResizableImageComponent(inProps: ResizableImageComponentProps) {
140140
// Throttle our "on resize" handler, since the event fires very rapidly during
141141
// dragging, so rendering would end up stuttering a bit without a throttle
142142
throttle(
143+
// eslint-disable-next-line react-hooks/refs -- ref is used in a memoized throttle callback (event handler), not during render
143144
(event: MouseEvent) => {
144145
if (!imageRef.current) {
145146
return;

0 commit comments

Comments
 (0)