355const PrintBody = forwardRef<HTMLButtonElement, PolyButtonProps>((props, ref) => {
356 /** Strip out unused props to prevent react errors */
357 const { organizationId: _org, serviceId: _serv, isMenu: _isMenu, ...restProps } = props
358 return <Box component='button' ref={ref} onClick={() => window.print()} {...restProps} />359})
360
361PrintBody.displayName = 'Print'
340 copiedToClipboard()
341 }
342
343 return <Box component='button' ref={ref} onClick={handleCopy} {...restProps} />344})
345
346CopyToClipBoard.displayName = 'CopyToClipboard'
298 <Button
299 className={isMenu ? classes.icon : classes.button}
300 radius='md'
301 onClick={() => removeItem.mutate({ id: singleListId, organizationId, serviceId })}302 >
303 {ButtonInner}
304 </Button>
207 onError: () => {
208 if (isSaved) return
209 setMenuChildren(
210 <Menu.Item onClick={() => savedLists.refetch()} closeMenuOnClick={false}>211 <Center>{t('retry')}</Center>
212 </Menu.Item>
213 )
157 }
158 }
159
160 return <Menu.Item onClick={() => clickHandler()}>{name}</Menu.Item>161}
162
163export const SaveToggleButton = forwardRef<HTMLDivElement, SaveToggleButtonProps>(
Using .bind()
or passing local callback functions as props to react component incurs a performance overhead.
Consider using React.useCallback
, or if possible, moving the callback definition outside the component.
EXCEPTIONS: This rule may not apply if your react component is only rendered once, or if your application is not performance sensitive. In such cases, consider adding a skipcq to prevent DeepSource from raising this issue on a single component. Alternatively, for small applications, you could add this issue in the ignore rules section.
Note that the performance overhead is not determined by the size of the callback function, but instead the number of times the component is rendered.
If the callback passed to a prop is local to the render function, it will get recreated every time the component renders.
This affects performance by causing unnecessary re-renders if a brand new function is passed as a property to a component that uses a reference equality check on the property to determine if it should update.
Using the useCallback
hook on functional components, or a method on class components is more performant.
Bad Practice
function CardWrapper() {
// the function `handleClick` is recreated every time
// a `CardWrapper` component is rendered.
const handleClick = (e) => displayCardDetails(e)
return <Card onClick={handleClick} />
}
function CardWrapper_() {
return <Card onClick={(e) => displayCardDetails(e)} />
}
class _CardWrapper extends React.Component {
render() {
return <Card onClick={(e) => displayCardDetails(e)} />
}
}
Recommended
function CardWrapper() {
// `handleClick` is no longer recreated on every render.
const handleClick = React.useCallback((e) => displayCardDetails(e))
return <Card onClick={handleClick} />
}
class CardWrapper_ extends React.Component {
handleClick(e) {
displayCardDetails(e)
}
render() {
return <Card onClick={this.handleClick} />
}
}