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

refactor: useEventNotStable type #2741

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion react/src/components/DynamicStepInputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DynamicInputNumber: React.FC<DynamicInputNumberProps> = ({
const [key, updateKey] = useUpdatableState('first');
useEffect(() => {
setTimeout(() => {
updateKey(value);
updateKey(value.toString());
}, 0);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
2 changes: 1 addition & 1 deletion react/src/components/DynamicUnitInputNumberWithSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const DynamicUnitInputNumberWithSlider: React.FC<
const [key, updateKey] = useUpdatableState('first');
useEffect(() => {
setTimeout(() => {
updateKey(value);
updateKey(value?.toString());
}, 0);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
15 changes: 11 additions & 4 deletions react/src/components/VFolderTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ import { ColumnsType } from 'antd/lib/table';
import graphql from 'babel-plugin-relay/macro';
import dayjs from 'dayjs';
import _ from 'lodash';
import React, { useEffect, useMemo, useState, useTransition } from 'react';
import React, {
useCallback,
useEffect,
useMemo,
useState,
useTransition,
} from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { useLazyLoadQuery } from 'react-relay';

Expand Down Expand Up @@ -244,16 +250,17 @@ const VFolderTable: React.FC<VFolderTableProps> = ({
* @param input - The input path to be converted.
* @returns The aliased path based on the name and input.
*/
const inputToAliasPath = useEventNotStable(
const inputToAliasPath = useCallback(
(name: VFolderKey, input?: string) => {
if (_.isEmpty(input)) {
if (input === undefined || input === '') {
return `${aliasBasePath}${name}`;
} else if (input?.startsWith('/')) {
} else if (input.startsWith('/')) {
return input;
} else {
return `${aliasBasePath}${input}`;
}
},
[aliasBasePath],
);
Comment on lines +253 to 264
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood that the reason you used the useCallback hook is to prevent the component from declaring a function with new memory allocation when the component is re-rendered, but is it always a good idea to declare the function using useCallback even if the computation of the function is not heavy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not necessary in that case. But let's keep this code to distinguish that aliasBasePath is a dependency.


const handleAliasUpdate = useEventNotStable(() => {
Expand Down
10 changes: 6 additions & 4 deletions react/src/hooks/useEventNotStable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useCallback, useLayoutEffect, useRef } from 'react';

export function useEventNotStable(handler: (props: any) => void) {
export function useEventNotStable<Args extends unknown[], Return>(
handler: (...args: Args) => Return,
) {
// eslint-disable-next-line
const handlerRef = useRef<Function | null>(null);
const handlerRef = useRef<typeof handler | undefined>(undefined);

// In a real implementation, this would run before layout effects
useLayoutEffect(() => {
Expand All @@ -11,9 +13,9 @@ export function useEventNotStable(handler: (props: any) => void) {

// eslint-disable-next-line
// @ts-ignore
return useCallback((...args) => {
return useCallback((...args: Args) => {
// In a real implementation, this would throw if called during render
const fn = handlerRef.current;
return fn ? fn(...args) : null;
return fn ? fn(...args) : undefined;
}, []);
}
Loading