Skip to content

Commit 3e87486

Browse files
using ref instead of closures
1 parent b680534 commit 3e87486

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/target_table.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,33 @@ export default function TargetTable(props: TargetTableProps) {
235235
const [editTarget, setEditTarget] = React.useState<Target>(row);
236236
const [count, setCount] = React.useState(0); //prevents scroll update from triggering save
237237
const [hasCatalog, setHasCatalog] = React.useState(row.tic_id || row.gaia_id ? true : false);
238+
const editTargetRef = React.useRef<Target>(editTarget);
239+
const countRef = React.useRef<number>(count);
240+
238241
const errors = React.useMemo<ErrorObject<string, Record<string, any>, unknown>[]>(() => {
239242
return validate_sanitized_target(row);
240243
}, [editTarget, count])
241244

242245
const apiRef = useGridApiContext();
243246

244-
const handleRowChange = async (override = false) => {
245-
if (count > 0 || override) {
247+
// Update refs when state changes
248+
React.useEffect(() => {
249+
editTargetRef.current = editTarget;
250+
countRef.current = count;
251+
}, [editTarget, count]);
252+
253+
const handleRowChange = React.useCallback(async (override = false) => {
254+
if (countRef.current > 0 || override) {
246255
let newTgt: Target | undefined = undefined
247-
const isEdited = editTarget.status?.includes('EDITED')
248-
if (isEdited) newTgt = await edit_target(editTarget)
249-
processRowUpdate(editTarget) //TODO: May want to wait till save is successful
256+
const isEdited = editTargetRef.current.status?.includes('EDITED')
257+
if (isEdited) newTgt = await edit_target(editTargetRef.current)
258+
processRowUpdate(editTargetRef.current) //TODO: May want to wait till save is successful
250259
if (newTgt) {
251260
newTgt.tic_id || newTgt.gaia_id && setHasCatalog(true)
252261
debounced_edit_click(id)
253262
}
254263
}
255-
}
264+
}, [id])
256265

257266
const debouncedHandleRowChange = useDebounceCallback(handleRowChange, 2000)
258267

0 commit comments

Comments
 (0)