Skip to content

Commit

Permalink
fix: prevent infinite rerenders in useIncomingWebhooks (#6176)
Browse files Browse the repository at this point in the history
React can sometimes be non-intuitive and behave erratically due to the
way it detects changes in hook dependencies.

This prevents infinite re-renders from `useIncomingWebhooks` by using a
static `DEFAULT_DATA` constant, so that its reference is always the
same, so no changes are detected when there are none.

Unrelated scouting, but this PR also removes an unneeded dependency in
the memoized columns in `ProjectActionsTable`.
  • Loading branch information
nunogois committed Feb 9, 2024
1 parent b77f312 commit 13df715
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Expand Up @@ -182,7 +182,7 @@ export const ProjectActionsTable = ({
disableSortBy: true,
},
],
[actions, incomingWebhooks, serviceAccounts],
[incomingWebhooks, serviceAccounts],
);

const [initialState] = useState({
Expand Down
Expand Up @@ -8,13 +8,17 @@ import { useUiFlag } from 'hooks/useUiFlag';

const ENDPOINT = 'api/admin/incoming-webhooks';

const DEFAULT_DATA = {
incomingWebhooks: [],
};

export const useIncomingWebhooks = () => {
const { isEnterprise } = useUiConfig();
const incomingWebhooksEnabled = useUiFlag('incomingWebhooks');

const { data, error, mutate } = useConditionalSWR(
isEnterprise() && incomingWebhooksEnabled,
{ incomingWebhooks: [] },
DEFAULT_DATA,
formatApiPath(ENDPOINT),
fetcher,
);
Expand Down

0 comments on commit 13df715

Please sign in to comment.