Skip to content

Commit c73a438

Browse files
committed
use ref and remove useEffect dep
1 parent 37548d5 commit c73a438

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

packages/compass-crud/src/components/json-editor.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ const JSONEditor: React.FunctionComponent<JSONEditorProps> = ({
8181
);
8282
const [initialValue] = useState<string>(() => doc.toEJSON());
8383
const [containsErrors, setContainsErrors] = useState<boolean>(false);
84+
const setModifiedEJSONStringRef = useRef<(value: string | null) => void>(
85+
doc.setModifiedEJSONString.bind(doc)
86+
);
8487

8588
useEffect(() => {
89+
const setModifiedEJSONString = setModifiedEJSONStringRef.current;
8690
return () => {
8791
// When this component is used in virtualized list, the editor is
8892
// unmounted on scroll and if the user is editing the document, the
8993
// editor value is lost. This is a way to keep track of the editor
9094
// value when the it's unmounted and is restored on next mount.
91-
doc.setModifiedEJSONString(editing ? value : null);
95+
setModifiedEJSONString(editing ? value : null);
9296
};
93-
}, [value, editing, doc]);
97+
}, [value, editing]);
9498

9599
const handleCopy = useCallback(() => {
96100
copyToClipboard?.(doc);

0 commit comments

Comments
 (0)