-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LG-4361 : Avoid most infinite render loops in ControlledToast (#2412)
* Avoid most infinite render loops in ControlledToast * Add changeset
- Loading branch information
1 parent
d70758d
commit cadc11b
Showing
4 changed files
with
77 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@leafygreen-ui/toast': patch | ||
--- | ||
|
||
Fixed the controlled Toast component to avoid most infinite render loops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/toast/src/ControlledToast/useStableControlledToastProps.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useCallback, useRef } from 'react'; | ||
import isEqual from 'lodash/isEqual'; | ||
|
||
import { ControlledToastProps } from './ControlledToast.types'; | ||
|
||
export default function useStableControlledToastProps( | ||
props: Omit<ControlledToastProps, 'open'>, | ||
) { | ||
// onClose is the only function prop, so handle it separately here | ||
// NOTE: Any functions inside props in the toast's title or description can still theoretically cause a render loop | ||
// unless they use useCallback | ||
const onCloseRef = useRef(props.onClose); | ||
onCloseRef.current = props.onClose; | ||
|
||
// A function that maintains a consistent identity and always calls the most recent version of the prop | ||
const stableOnClose = useCallback< | ||
NonNullable<ControlledToastProps['onClose']> | ||
>((...args) => onCloseRef.current?.(...args), []); | ||
|
||
const propsWithStableOnClose = { | ||
...props, | ||
onClose: stableOnClose, | ||
}; | ||
|
||
const lastPropsRef = useRef<Omit<ControlledToastProps, 'open'> | null>(null); | ||
const lastProps = lastPropsRef.current; | ||
const stableProps = | ||
lastProps != null && isEqual(propsWithStableOnClose, lastProps) | ||
? lastProps | ||
: propsWithStableOnClose; | ||
lastPropsRef.current = stableProps; | ||
|
||
return stableProps; | ||
} |