Skip to content

Commit 0a3acb8

Browse files
committed
refactor: resolve hook violations
add missing dependencies update Removes the following warnings: > $ npm run lint > > [email protected] lint > next lint > > > ./src/app/login/githublogin.tsx > 32:6 Warning: React Hook useEffect has a missing dependency: 'searchParams'. Either include it or remove the dependency array. react-hooks/exhaustive-deps > > ./src/components/Contribute/Knowledge/Github/index.tsx > 429:9 Warning: The 'knowledgeFormData' object makes the dependencies of useEffect Hook (at line 449) change on every render. To fix this, wrap the initialization of 'knowledgeFormData' in its own useMemo() Hook. react-hooks/exhaustive-deps > > ./src/components/Contribute/Knowledge/Native/index.tsx > 428:9 Warning: The 'knowledgeFormData' object makes the dependencies of useEffect Hook (at line 448) change on every render. To fix this, wrap the initialization of 'knowledgeFormData' in its own useMemo() Hook. react-hooks/exhaustive-deps > > ./src/components/Contribute/Skill/Github/index.tsx > 327:9 Warning: The 'skillFormData' object makes the dependencies of useEffect Hook (at line 341) change on every render. To fix this, wrap the initialization of 'skillFormData' in its own useMemo() Hook. react-hooks/exhaustive-deps > > ./src/components/Contribute/Skill/Native/index.tsx > 304:9 Warning: The 'skillFormData' object makes the dependencies of useEffect Hook (at line 318) change on every render. To fix this, wrap the initialization of 'skillFormData' in its own useMemo() Hook. react-hooks/exhaustive-deps > > ./src/components/GithubAccessPopup/index.tsx > 28:6 Warning: React Hook React.useEffect has a missing dependency: 'onAccept'. Either include it or remove the dependency array. If 'onAccept' changes too often, find the parent component that defines it and wrap that definition in useCallback. react-hooks/exhaustive-deps > > ./src/components/PathService/PathService.tsx > 82:6 Warning: React Hook useEffect has a missing dependency: 'path'. Either include it or remove the dependency array. If 'setInputValue' needs the current value of 'path', you can also switch to useReducer instead of useState and read 'path' in the reducer. react-hooks/exhaustive-deps > 94:6 Warning: React Hook useEffect has missing dependencies: 'fetchData', 'handlePathChange', and 'validatePath'. Either include them or remove the dependency array. If 'handlePathChange' changes too often, find the parent component that defines it and wrap that definition in useCallback. react-hooks/exhaustive-deps > > info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/app/api-reference/config/eslint#disabling-rules Signed-off-by: Alexander Alemayhu <[email protected]>
1 parent dc7ad07 commit 0a3acb8

File tree

7 files changed

+136
-89
lines changed

7 files changed

+136
-89
lines changed

src/app/login/githublogin.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const GithubLogin: React.FC = () => {
2929
setErrorMsg(errorMessage);
3030
setShowError(true);
3131
}
32-
}, []);
32+
}, [searchParams]);
3333

3434
const handleGitHubLogin = () => {
3535
signIn('github', { callbackUrl: '/' }); // Redirect to home page after login

src/components/Contribute/Knowledge/Github/index.tsx

+36-17
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,42 @@ export const KnowledgeFormGithub: React.FunctionComponent<KnowledgeFormProps> =
426426
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
427427
};
428428

429-
const knowledgeFormData: KnowledgeFormData = {
430-
email: email,
431-
name: name,
432-
submissionSummary: submissionSummary,
433-
domain: domain,
434-
documentOutline: documentOutline,
435-
filePath: filePath,
436-
seedExamples: seedExamples,
437-
knowledgeDocumentRepositoryUrl: knowledgeDocumentRepositoryUrl,
438-
knowledgeDocumentCommit: knowledgeDocumentCommit,
439-
documentName: documentName,
440-
titleWork: titleWork,
441-
linkWork: linkWork,
442-
revision: revision,
443-
licenseWork: licenseWork,
444-
creators: creators
445-
};
429+
const knowledgeFormData: KnowledgeFormData = useMemo(
430+
() => ({
431+
email: email,
432+
name: name,
433+
submissionSummary: submissionSummary,
434+
domain: domain,
435+
documentOutline: documentOutline,
436+
filePath: filePath,
437+
seedExamples: seedExamples,
438+
knowledgeDocumentRepositoryUrl: knowledgeDocumentRepositoryUrl,
439+
knowledgeDocumentCommit: knowledgeDocumentCommit,
440+
documentName: documentName,
441+
titleWork: titleWork,
442+
linkWork: linkWork,
443+
revision: revision,
444+
licenseWork: licenseWork,
445+
creators: creators
446+
}),
447+
[
448+
email,
449+
name,
450+
submissionSummary,
451+
domain,
452+
documentOutline,
453+
filePath,
454+
seedExamples,
455+
knowledgeDocumentRepositoryUrl,
456+
knowledgeDocumentCommit,
457+
documentName,
458+
titleWork,
459+
linkWork,
460+
revision,
461+
licenseWork,
462+
creators
463+
]
464+
);
446465

447466
useEffect(() => {
448467
setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData));

src/components/Contribute/Knowledge/Native/index.tsx

+36-17
Original file line numberDiff line numberDiff line change
@@ -425,23 +425,42 @@ export const KnowledgeFormNative: React.FunctionComponent<KnowledgeFormProps> =
425425
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
426426
};
427427

428-
const knowledgeFormData: KnowledgeFormData = {
429-
email: email,
430-
name: name,
431-
submissionSummary: submissionSummary,
432-
domain: domain,
433-
documentOutline: documentOutline,
434-
filePath: filePath,
435-
seedExamples: seedExamples,
436-
knowledgeDocumentRepositoryUrl: knowledgeDocumentRepositoryUrl,
437-
knowledgeDocumentCommit: knowledgeDocumentCommit,
438-
documentName: documentName,
439-
titleWork: titleWork,
440-
linkWork: linkWork,
441-
revision: revision,
442-
licenseWork: licenseWork,
443-
creators: creators
444-
};
428+
const knowledgeFormData: KnowledgeFormData = useMemo(
429+
() => ({
430+
email: email,
431+
name: name,
432+
submissionSummary: submissionSummary,
433+
domain: domain,
434+
documentOutline: documentOutline,
435+
filePath: filePath,
436+
seedExamples: seedExamples,
437+
knowledgeDocumentRepositoryUrl: knowledgeDocumentRepositoryUrl,
438+
knowledgeDocumentCommit: knowledgeDocumentCommit,
439+
documentName: documentName,
440+
titleWork: titleWork,
441+
linkWork: linkWork,
442+
revision: revision,
443+
licenseWork: licenseWork,
444+
creators: creators
445+
}),
446+
[
447+
email,
448+
name,
449+
submissionSummary,
450+
domain,
451+
documentOutline,
452+
filePath,
453+
seedExamples,
454+
knowledgeDocumentRepositoryUrl,
455+
knowledgeDocumentCommit,
456+
documentName,
457+
titleWork,
458+
linkWork,
459+
revision,
460+
licenseWork,
461+
creators
462+
]
463+
);
445464

446465
useEffect(() => {
447466
setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData));

src/components/Contribute/Skill/Github/index.tsx

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/components/Contribute/Skill/Github/index.tsx
22
'use client';
3-
import React, { useEffect, useState } from 'react';
3+
import React, { useEffect, useState, useMemo } from 'react';
44
import './skills.css';
55
import { Alert, AlertActionCloseButton, AlertGroup } from '@patternfly/react-core/dist/dynamic/components/Alert';
66
import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
@@ -324,17 +324,20 @@ export const SkillFormGithub: React.FunctionComponent<SkillFormProps> = ({ skill
324324
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
325325
};
326326

327-
const skillFormData: SkillFormData = {
328-
email: email,
329-
name: name,
330-
submissionSummary: submissionSummary,
331-
documentOutline: documentOutline,
332-
filePath: filePath,
333-
seedExamples: seedExamples,
334-
titleWork: titleWork,
335-
licenseWork: licenseWork,
336-
creators: creators
337-
};
327+
const skillFormData: SkillFormData = useMemo(
328+
() => ({
329+
email: email,
330+
name: name,
331+
submissionSummary: submissionSummary,
332+
documentOutline: documentOutline,
333+
filePath: filePath,
334+
seedExamples: seedExamples,
335+
titleWork: titleWork,
336+
licenseWork: licenseWork,
337+
creators: creators
338+
}),
339+
[email, name, submissionSummary, documentOutline, filePath, seedExamples, titleWork, licenseWork, creators]
340+
);
338341

339342
useEffect(() => {
340343
setDisableAction(!checkSkillFormCompletion(skillFormData));

src/components/Contribute/Skill/Native/index.tsx

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/components/Contribute/Skill/Native/index.tsx
22
'use client';
3-
import React, { useEffect, useState } from 'react';
3+
import React, { useEffect, useState, useMemo } from 'react';
44
import './skills.css';
55
import { Alert, AlertActionCloseButton, AlertGroup } from '@patternfly/react-core/dist/dynamic/components/Alert';
66
import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
@@ -301,17 +301,20 @@ export const SkillFormNative: React.FunctionComponent<SkillFormProps> = ({ skill
301301
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
302302
};
303303

304-
const skillFormData: SkillFormData = {
305-
email: email,
306-
name: name,
307-
submissionSummary: submissionSummary,
308-
documentOutline: documentOutline,
309-
filePath: filePath,
310-
seedExamples: seedExamples,
311-
titleWork: titleWork,
312-
licenseWork: licenseWork,
313-
creators: creators
314-
};
304+
const skillFormData: SkillFormData = useMemo(
305+
() => ({
306+
email,
307+
name,
308+
submissionSummary,
309+
documentOutline,
310+
filePath,
311+
seedExamples,
312+
titleWork,
313+
licenseWork,
314+
creators
315+
}),
316+
[email, name, submissionSummary, documentOutline, filePath, seedExamples, titleWork, licenseWork, creators]
317+
);
315318

316319
useEffect(() => {
317320
setDisableAction(!checkSkillFormCompletion(skillFormData));

src/components/GithubAccessPopup/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const GithubAccessPopup: React.FunctionComponent<Props> = ({ onAccept }) => {
2525
}
2626
};
2727
showPopupWarning();
28-
}, []);
28+
}, [onAccept]);
2929

3030
const setDecisionAndClose = () => {
3131
setIsOpen(false);

src/components/PathService/PathService.tsx

+32-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// /src/components/PathService.tsx
2-
import React, { useState, useEffect, useRef } from 'react';
2+
import React, { useState, useEffect, useRef, useCallback } from 'react';
33
import { SearchInput } from '@patternfly/react-core/dist/dynamic/components/SearchInput';
44
import { List } from '@patternfly/react-core/dist/dynamic/components/List';
55
import { ListItem } from '@patternfly/react-core/dist/dynamic/components/List';
@@ -23,40 +23,43 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
2323
const inputRef = useRef<HTMLInputElement>(null);
2424
const [validPath, setValidPath] = React.useState<ValidatedOptions>();
2525

26-
const validatePath = () => {
26+
const validatePath = useCallback(() => {
2727
if (inputValue.trim().length > 0) {
2828
setValidPath(ValidatedOptions.success);
2929
return;
3030
}
3131
setValidPath(ValidatedOptions.error);
32-
};
33-
34-
const fetchData = async (subpath: string) => {
35-
try {
36-
const response = await fetch('/api/tree', {
37-
method: 'POST',
38-
headers: {
39-
'Content-Type': 'application/json'
40-
},
41-
body: JSON.stringify({ root_path: rootPath, dir_name: subpath })
42-
});
43-
44-
if (!response.ok) {
45-
console.warn('Failed to get path service tree for subpath ( ' + subpath + ' ) from server.');
46-
}
32+
}, [inputValue]);
4733

48-
const result = await response.json();
49-
// set items to be displayed in the dropdown
50-
if (result.data === null || result.data.length === 0) {
34+
const fetchData = useCallback(
35+
async (subpath: string) => {
36+
try {
37+
const response = await fetch('/api/tree', {
38+
method: 'POST',
39+
headers: {
40+
'Content-Type': 'application/json'
41+
},
42+
body: JSON.stringify({ root_path: rootPath, dir_name: subpath })
43+
});
44+
45+
if (!response.ok) {
46+
console.warn('Failed to get path service tree for subpath ( ' + subpath + ' ) from server.');
47+
}
48+
49+
const result = await response.json();
50+
// set items to be displayed in the dropdown
51+
if (result.data === null || result.data.length === 0) {
52+
setItems([]);
53+
return;
54+
}
55+
setItems(result.data.map((item: string) => item.valueOf()));
56+
} catch (error) {
57+
console.warn('Error fetching path service data:', error);
5158
setItems([]);
52-
return;
5359
}
54-
setItems(result.data.map((item: string) => item.valueOf()));
55-
} catch (error) {
56-
console.warn('Error fetching path service data:', error);
57-
setItems([]);
58-
}
59-
};
60+
},
61+
[rootPath]
62+
);
6063

6164
useEffect(() => {
6265
setInputValue('');
@@ -79,7 +82,7 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
7982
return () => {
8083
window.removeEventListener('keydown', handleEsc);
8184
};
82-
}, []);
85+
}, [path]);
8386

8487
useEffect(() => {
8588
// check if input value is empty or ends with a slash
@@ -91,7 +94,7 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
9194
setItems([]);
9295
}
9396
validatePath();
94-
}, [inputValue]);
97+
}, [inputValue, fetchData, handlePathChange, validatePath]);
9598

9699
const handleChange = (value: string) => {
97100
setInputValue(value);

0 commit comments

Comments
 (0)