Fix React setState error when navigating out of Branches page while divergence calculation is still in progress (#636)

This commit is contained in:
Tan Nhu 2023-09-29 20:11:08 +00:00 committed by Harness
parent c5e5d33e4d
commit 9bb2ca1f5e
1 changed files with 21 additions and 6 deletions

View File

@ -15,7 +15,17 @@
*/
import React, { useEffect, useMemo, useState } from 'react'
import { Container, TableV2 as Table, Text, Avatar, Tag, useToaster, StringSubstitute } from '@harnessio/uicore'
import {
Container,
TableV2 as Table,
Text,
Avatar,
Tag,
useToaster,
StringSubstitute,
useIsMounted
} from '@harnessio/uicore'
import { noop } from 'lodash-es'
import { Color, Intent } from '@harnessio/design-system'
import type { CellProps, Column } from 'react-table'
import { Link, useHistory } from 'react-router-dom'
@ -60,15 +70,20 @@ export function BranchesContent({ repoMetadata, searchTerm = '', branches, onDel
requests: branches?.map(branch => ({ from: branch.name, to: repoMetadata.default_branch }))
}
}, [repoMetadata, branches])
const isMounted = useIsMounted()
useEffect(() => {
if (branchDivergenceRequestBody.requests?.length) {
if (isMounted.current && branchDivergenceRequestBody.requests?.length) {
setDivergence([])
getBranchDivergence(branchDivergenceRequestBody).then((response: RepoCommitDivergence[]) => {
setDivergence(response)
})
getBranchDivergence(branchDivergenceRequestBody)
.then((response: RepoCommitDivergence[]) => {
if (isMounted.current) {
setDivergence(response)
}
})
.catch(noop)
}
}, [getBranchDivergence, branchDivergenceRequestBody])
}, [getBranchDivergence, branchDivergenceRequestBody, isMounted])
const columns: Column<RepoBranch>[] = useMemo(
() => [