Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store function in ref to avoid having to mention it as a dependency #18

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useRef } from "react";
import { clsx } from "clsx";
import { ILocation } from "../types";
import { kInitialDimensions, kVersion, kPluginName, kDefaultOnAttributes, kSimulationTabDimensions, kDataContextName } from "../constants";
Expand All @@ -21,6 +21,8 @@ export const App: React.FC = () => {
const [dataContext, setDataContext] = useState<any>(null);

const { getUniqueLocationsInCodapData } = useCodapData();
// Store a ref to getUniqueLocationsInCodapData so we can call inside useEffect without triggering unnecessary re-runs
const getUniqueLocationsRef = useRef(getUniqueLocationsInCodapData);

useEffect(() => {
const initialize = async () => {
Expand All @@ -42,15 +44,15 @@ export const App: React.FC = () => {
const casesDeleted = values.operation === "selectCases" && values.result.cases.length === 0 && values.result.success;

if ( casesDeleted ) {
const uniqeLocations = await getUniqueLocationsInCodapData();
const uniqeLocations = await getUniqueLocationsRef.current();
if (uniqeLocations) setLocations(uniqeLocations);
}
};
addDataContextChangeListener(kDataContextName, casesDeletedFromCodapListener);
};

initialize();
}, [getUniqueLocationsInCodapData]);
}, []);

const handleTabClick = (tab: "location" | "simulation") => {
setActiveTab(tab);
Expand Down
Loading