Skip to main content

useCallback hook

Proposed change

Callback functions created inside React components and sent to sub components (or other usage outside of the component) will be recreated every time the component is rendered. This leads to unnecessary re-renders in all sub components receiving that function through props.

How it looks now

import react from 'react';

function SomeComponent() {
function handleCloseDrawer() {
setIsDrawerOpen(false);
}

return <SubComponent onCloseDrawer={handleCloseDrawer} />;
}

Solution

import react, { useCallback } from 'react';

function SomeComponent() {
const handleCloseDrawer = useCallback(() => {
setIsDrawerOpen(false);
}, [setIsDrawerOpen]);

return <SubComponent onCloseDrawer={handleCloseDrawer} />;
}

Motivation

Unnecessary re-renders lowers performance, but could also potentially lead to unwanted effects because components render without having any actual changes (like if the callback function is recreated, but no other props/state has changed) by triggering useEffect hooks or other logic triggered by re-renders.

A practical way to test if a changes leads to re-renders is to use the React extension for Chrome, and enable highlighting re-renders in the UI. If something you do in the UI, like entering text in an input field or clicking a button leads to re-renders in parts of the page that should not be affecetd by the action taken, it could be caused by unnecessary re-renders.

More info about useCallback can be found in the React docs

Proposed transition strategy

Rewrite existing apps to use useCallback where it helps reduce unnecessary re-renders in all apps/libs. Update the following list after implementation:

Apps

  • apps/pm-netbank/accounts
  • apps/pm-netbank/ask
  • apps/pm-netbank/bank-swap
  • apps/pm-netbank/bankid
  • apps/pm-netbank/become-customer
  • apps/pm-netbank/cards
  • apps/pm-netbank/epk-flex
  • apps/pm-netbank/fal-agreement
  • apps/pm-netbank/funds
  • apps/pm-netbank/funds-pricing
  • apps/pm-netbank/hunting
  • apps/pm-netbank/insurance
  • apps/pm-netbank/invoices
  • apps/pm-netbank/loans
  • apps/pm-netbank/login-route
  • apps/pm-netbank/overview
  • apps/pm-netbank/payment-agreements
  • apps/pm-netbank/payments
  • apps/pm-netbank/pension
  • apps/pm-netbank/pre-qualification-letters
  • apps/pm-netbank/property
  • apps/pm-netbank/rammelaan
  • apps/pm-netbank/root-app
  • apps/pm-netbank/savings
  • apps/pm-netbank/spendings
  • apps/pm-netbank/transfers

Libs

  • libs/pm-netbank/charts
  • libs/pm-netbank/components
  • libs/pm-netbank/constants
  • libs/pm-netbank/endpoints
  • libs/pm-netbank/factories
  • libs/pm-netbank/feature-toggle
  • libs/pm-netbank/feedback-form
  • libs/pm-netbank/form-validation
  • libs/pm-netbank/forms
  • libs/pm-netbank/forms-renderers
  • libs/pm-netbank/hooks
  • libs/pm-netbank/logging
  • libs/pm-netbank/notifications
  • libs/pm-netbank/page-layout
  • libs/pm-netbank/providers
  • libs/pm-netbank/routes
  • libs/pm-netbank/services
  • libs/pm-netbank/styles
  • libs/pm-netbank/test-helpers
  • libs/pm-netbank/types