R: Prevent potential infinite loops

This commit is contained in:
Ivan 2025-11-06 15:39:28 +03:00
parent a226d014f4
commit 45a9bc6787
2 changed files with 14 additions and 5 deletions

View File

@ -52,9 +52,13 @@ export function EditorConstituenta() {
const role = useRoleStore(state => state.role);
const listHeight = useFitHeight(!isNarrow ? '8.2rem' : role !== UserRole.READER ? '42rem' : '35rem', '10rem');
const prevActiveCstId = useRef<number | null>(null);
useEffect(() => {
if (activeCst && selected.length !== 1) {
setSelected([activeCst.id]);
if (activeCst && prevActiveCstId.current !== activeCst.id) {
prevActiveCstId.current = activeCst.id;
if (selected.length !== 1 || selected[0] !== activeCst.id) {
setSelected([activeCst.id]);
}
}
}, [activeCst, selected, setSelected]);

View File

@ -1,9 +1,14 @@
import { useEffect, useMemo } from 'react';
import { useEffect, useMemo, useRef } from 'react';
export function useResetOnChange<T>(deps: T[], resetFn: () => void) {
const depsKey = useMemo(() => JSON.stringify(deps), [deps]);
const resetFnRef = useRef(resetFn);
useEffect(() => {
resetFn();
}, [depsKey, resetFn]);
resetFnRef.current = resetFn;
}, [resetFn]);
useEffect(() => {
resetFnRef.current();
}, [depsKey]);
}