From c641f31505197aefaf0e49095805ba70863b6c01 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Wed, 21 Aug 2024 11:01:58 -0400 Subject: [PATCH 1/2] refact: rewrite some effect-state updates as functional updates --- src/components/manager/drs/ManagerDRSContent.js | 4 ++-- src/components/manager/projects/Project.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/manager/drs/ManagerDRSContent.js b/src/components/manager/drs/ManagerDRSContent.js index 7c1522330..18b156beb 100644 --- a/src/components/manager/drs/ManagerDRSContent.js +++ b/src/components/manager/drs/ManagerDRSContent.js @@ -240,8 +240,8 @@ const ManagerDRSContent = () => { }, [performSearch, searchValue]); useEffect(() => { - setSelectedRowKeys(selectedRowKeys.filter((k) => k in objectsByID)); - }, [selectedRowKeys, objectsByID]); + setSelectedRowKeys((s) => s.filter((k) => k in objectsByID)); + }, [objectsByID]); const onDeleteSelected = useCallback(() => { Modal.confirm({ diff --git a/src/components/manager/projects/Project.js b/src/components/manager/projects/Project.js index 45d509bc1..e9ab98a5b 100644 --- a/src/components/manager/projects/Project.js +++ b/src/components/manager/projects/Project.js @@ -61,13 +61,13 @@ const Project = ({ useEffect(() => { if (value) { - setProjectState({ - ...projectState, + setProjectState((ps) => ({ + ...ps, ...value, data_use: simpleDeepCopy(value.data_use || INITIAL_DATA_USE_VALUE), - }); + })); } - }, [value, projectState]); + }, [value]); const handleSave = useCallback(() => { const form = editingForm.current; From c9b59cb56cd8e51b39f5f0098519f9221f656ccb Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Wed, 21 Aug 2024 11:26:34 -0400 Subject: [PATCH 2/2] refact: more use of functional state updates --- src/components/SiteHeader.js | 4 ++-- src/components/discovery/DiscoveryQueryBuilder.js | 4 ++-- src/components/explorer/IndividualTracks.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/SiteHeader.js b/src/components/SiteHeader.js index 66932600f..14493d68f 100644 --- a/src/components/SiteHeader.js +++ b/src/components/SiteHeader.js @@ -67,8 +67,8 @@ const SiteHeader = () => { const [modalVisible, setModalVisible] = useState(false); const toggleModalVisibility = useCallback(() => { - setModalVisible(!modalVisible); - }, [modalVisible]); + setModalVisible((v) => !v); + }, []); const performSignOut = usePerformSignOut(); diff --git a/src/components/discovery/DiscoveryQueryBuilder.js b/src/components/discovery/DiscoveryQueryBuilder.js index 9260a3191..f35b5f550 100644 --- a/src/components/discovery/DiscoveryQueryBuilder.js +++ b/src/components/discovery/DiscoveryQueryBuilder.js @@ -121,8 +121,8 @@ const DiscoveryQueryBuilder = ({ activeDataset, dataTypeForms, requiredDataTypes ); const handleHelpAndSchemasToggle = useCallback(() => { - setSchemasModalShown(!schemasModalShown); - }, [schemasModalShown]); + setSchemasModalShown((s) => !s); + }, []); const handleSetFormRef = useCallback( (dataType, form) => { diff --git a/src/components/explorer/IndividualTracks.js b/src/components/explorer/IndividualTracks.js index d278cc579..899d86b63 100644 --- a/src/components/explorer/IndividualTracks.js +++ b/src/components/explorer/IndividualTracks.js @@ -270,7 +270,7 @@ const IndividualTracks = ({ individual }) => { if (!igvBrowserRef.current) return; const wasViewing = track.viewInIgv; - setAllTracks(allTracks.map((t) => (t.filename === track.filename ? { ...track, viewInIgv: !wasViewing } : t))); + setAllTracks((at) => at.map((t) => (t.filename === track.filename ? { ...track, viewInIgv: !wasViewing } : t))); if (wasViewing) { igvBrowserRef.current.removeTrackByName(track.filename); @@ -278,7 +278,7 @@ const IndividualTracks = ({ individual }) => { igvBrowserRef.current.loadTrack(buildIgvTrack(igvUrls, track)).catch(console.error); } }, - [allTracks, igvUrls], + [igvUrls], ); const storeIgvPosition = useCallback(