Skip to content

Commit

Permalink
Memoize callbacks and define exhaustive deps for hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed Sep 8, 2021
1 parent 9be3802 commit 7ccb6ab
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export const CloudBackup: React.FunctionComponent<Props> = ({
// An error should invalidate the previous state.
setIsComplete((!error && data?.isBackedUp) ?? false);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [error, isLoading, data]);
}, [setIsComplete, error, isLoading, data]);

if (isInitialRequest && isLoading) {
return <EuiLoadingContent data-test-subj="cloudBackupLoading" lines={3} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ const FixIssuesStep: FunctionComponent<OverviewStepProps> = ({ setIsComplete })

useEffect(() => {
setIsComplete(isEsFixed && isKibanaFixed);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isEsFixed, isKibanaFixed]);
}, [setIsComplete, isEsFixed, isKibanaFixed]);

return (
<EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ export const DeprecationsCountCheckpoint: FunctionComponent<Props> = ({
// An error should invalidate the previous state.
setHasNoDeprecationLogs(!error && !hasLogs);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [error, isLoading, hasLogs]);
}, [setHasNoDeprecationLogs, error, isLoading, hasLogs]);

if (isInitialRequest && isLoading) {
return <EuiLoadingContent lines={6} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { FunctionComponent, useEffect, useState } from 'react';
import React, { FunctionComponent, useEffect, useState, useCallback } from 'react';

import {
EuiSteps,
Expand Down Expand Up @@ -60,12 +60,14 @@ export const Overview: FunctionComponent = () => {
});

const isStepComplete = (step: OverviewStep) => completedStepsMap[step];
const setCompletedStep = (step: OverviewStep, isCompleted: boolean) => {
setCompletedStepsMap({
...completedStepsMap,
[step]: isCompleted,
});
};
const setCompletedStep = useCallback(() => {
return (step: OverviewStep, isCompleted: boolean) => {
setCompletedStepsMap({
...completedStepsMap,
[step]: isCompleted,
});
};
}, [completedStepsMap]);

return (
<EuiPageBody restrictWidth={true} data-test-subj="overview">
Expand Down Expand Up @@ -110,16 +112,22 @@ export const Overview: FunctionComponent = () => {
getBackupStep({
cloud,
isComplete: isStepComplete('backup'),
setIsComplete: setCompletedStep.bind(null, 'backup'),
setIsComplete: useCallback(() => setCompletedStep.bind(null, 'backup'), [
setCompletedStep,
]),
}),
getFixIssuesStep({
nextMajor,
isComplete: isStepComplete('fix_issues'),
setIsComplete: setCompletedStep.bind(null, 'fix_issues'),
setIsComplete: useCallback(() => setCompletedStep.bind(null, 'fix_issues'), [
setCompletedStep,
]),
}),
getFixLogsStep({
isComplete: isStepComplete('fix_logs'),
setIsComplete: setCompletedStep.bind(null, 'fix_logs'),
setIsComplete: useCallback(() => setCompletedStep.bind(null, 'fix_logs'), [
setCompletedStep,
]),
}),
getUpgradeStep({ docLinks, nextMajor }),
]}
Expand Down

0 comments on commit 7ccb6ab

Please sign in to comment.