Developer way

Why custom react hooks could destroy your app performance

Nadia Makarevich

Nadia Makarevich

Jan 24, 2022

Nadia MakarevichNadia Makarevich

Scary title, isn’t it? The sad part is that it’s true: for performance-sensitive apps custom React hooks can very easily turn into the biggest performance killer, if not written and used very carefully.

I’m not going to explain how to build and use hooks here, if you never built a hook before, the React docs have a pretty good introduction into it. What I want to focus on today is their performance implication for complicated apps.

Let’s build a modal dialog on custom hooks

Essentially, hooks are just advanced functions that allow developers to use things like state and context without creating new components. They are super useful when you need to share the same piece of logic that needs state between different parts of the app. With hooks came a new era in React development: never before our components were as slim and neat as with hooks, and separation of different concerns was as easy to achieve as with hooks.

Let’s for example, implement a modal dialog. With custom hooks, we can create a piece of beauty here.

First, let’s implement a “base” component, that doesn’t have any state, but just renders the dialog when isOpen prop is provided and triggers onClose callback when a click on a blanket underneath the dialog happens.

1type ModalProps = {
2 isOpen: boolean;
3 onClosed: () => void;
6export const ModalBase = ({ isOpen, onClosed }: ModalProps) => {
7 return isOpen ? (
8 <>
9 <div css={modalBlanketCss} onClick={onClosed} />
10 <div css={modalBodyCss}>Modal dialog content</div>
11 </>
12 ) : null;

Now to the state management, i.e. the “open dialog/close dialog” logic. In the “old” way we would usually implement a “smart” version of it, which handles the state management and accepts a component that is supposed to trigger the opening of the dialog as a prop. Something like this:

1export const ModalDialog = ({ trigger }) => {
2 const [isOpen, setIsOpen] = useState(false);
4 return (
5 <>
6 <div onClick={() => setIsOpen(true)}>{trigger}</div>
7 <ModalBase isOpen={isOpen} onClosed={() => setIsOpen(false)} />
8 </>
9 );

Which then will be used like this:

1<ModalDialog trigger={<button>Click me</button>} />

This is not a particularly pretty solution, we’re messing with the position and accessibility of the trigger component inside our modal dialog by wrapping it in a div. Not to mention that this unnecessary div will result in a slightly larger and messier DOM.

And now watch the magic. If we extract the “open/close” logic into a custom hook, render this component inside the hook, and expose API to control it as a return value from the hook, we can have the best of both worlds. In the hook we’ll have the “smart” dialog that handles its own state, but doesn’t mess with the trigger nor does it need one:

1export const useModal = () => {
2 const [isOpen, setIsOpen] = useState(false);
4 const open = () => setIsOpen(true);
5 const close = () => setIsOpen(false);
6 const Dialog = () => <ModalBase onClosed={close} isOpen={isOpen} />;
8 return { isOpen, Dialog, open, close };

And on the consumer side we’ll have a minimal amount of code while having the full control over what triggers the dialog:

1const ConsumerComponent = () => {
2 const { Dialog, open } = useModal();
4 return (
5 <>
6 <button onClick={open}>Click me</button>
7 <Dialog />
8 </>
9 );

If this isn’t perfection, I don’t know what is! 😍 See this beauty in codesandbox. Only don’t rush to use it in your apps right away, not until you read about its dark side 😅

Performance implications

In the previous article, where I covered in detail various patterns that lead to poor performance, I implemented a “slow” app: just a simple not optimized list of ~250 countries rendered on the page. But every interaction there causes the entire page to re-render, which makes it probably the slowest simple list ever existed. Here is the codesandbox, click on different countries in the list to see what I mean (if you’re on the latest Mac throttle your CPU a bit to get a better impression).

How to throttle CPU: in Chrome developer tools open “Performance” tab, and click on the “cog wheel” icon in the top right corner - it will open a small additional panel with throttling options.

I’m going to use our new perfect modal dialog there and see what happens. The code of the main Page component is relatively simple and looks like this:

1export const Page = ({ countries }: { countries: Country[] }) => {
2 const [selectedCountry, setSelectedCountry] = useState<Country>(countries[0]);
3 const [savedCountry, setSavedCountry] = useState<Country>(countries[0]);
4 const [mode, setMode] = useState<Mode>('light');
6 return (
7 <ThemeProvider value={{ mode }}>
8 <h1>Country settings</h1>
9 <button onClick={() => setMode(mode === 'light' ? 'dark' : 'light')}>Toggle theme</button>
10 <div className="content">
11 <CountriesList countries={countries} onCountryChanged={(c) => setSelectedCountry(c)} savedCountry={savedCountry} />
12 <SelectedCountry country={selectedCountry} onCountrySaved={() => setSavedCountry(selectedCountry)} />
13 </div>
14 </ThemeProvider>
15 );

And now I need a button near the “Toggle theme” button that would open a modal dialog with some future additional settings for this page. Luckily, now it can’t be simpler: add useModal hook at the top, add the button where it needs to be, and pass open callback to the button. The Page component barely changes and is still quite simple:

You probably already guessed the result 🙂 The slowest appearance of 2 empty divs ever existed 😱. See the codesandbox.

You see, what is happening here, is our useModal hook uses state. And as we know, state changes are one of the reasons why a component would re-render itself. This also applies to hooks - if the hook's state changes, the "host" component will re-render. And it makes total sense. If we look closely inside useModal hook, we’ll see that it’s just a nice abstraction around setState, it exists outside of the Dialog component. Essentially it’s no different than calling setState in the Page component directly.

And this is where the big danger of hooks is: yes, they help us make the API really nice. But what we did as a result, and the way of hooks is pretty much encouraging it, is essentially lifted state up from where it was supposed to be. And it’s entirely not noticeable unless you go inside the useModal implementation or have lots of experience with hooks and re-renders. I’m not even using the state directly in Page component, all I'm doing from its perspective is rendering a Dialog component and calling an imperative API to open it.

In the “old world”, the state would’ve been encapsulated in the slightly ugly Modal dialog with the trigger prop, and the Page component would’ve stayed intact when the button is clicked. Now the click on the button changes the state of the entire Page component, which causes it to re-render (which is super slow for this app). And the dialog can only appear when React is done with all the re-renders it caused, hence the big delay.

So, what can we do about it? We probably won’t have time and resources to fix the underlying performance of the Page component, as it would usually happen with the “real” apps. But at least we can make sure that the new feature doesn’t add to the performance problems and is fast by itself. All that we need to do here is just move the modal state “down”, away from the slow Page component:

1const SettingsButton = () => {
2 const { Dialog, open } = useModal();
4 return (
5 <>
6 <button onClick={open}>Open settings</button>
7 <Dialog />
8 </>
9 );

And in Page just render the SettingsButton:

1export const Page = ({ countries }: { countries: Country[] }) => {
2 // same as original page state
3 return (
4 <ThemeProvider value={{ mode }}>
5 // stays the same
6 <SettingsButton />
7 // stays the same
8 </ThemeProvider>
9 );

Now, when the button is clicked, only SettingsButton component will re-render, the slow Page component is unaffected. Essentially, we’re imitating the state model as it would’ve been in the “old” world while preserving the nice hooks-based API. See the codesandbox with the solution.

Adding more functionality to the useModal hook

Let’s make our hooks performance conversation slightly darker 🙂. Imagine, for example, you need to track the scroll event in the modal content. Maybe you want to send some analytics events when the users scroll through the text, to track reads. What will happen if I don’t want to introduce “smart” functionality to the BaseModal and do it in the useModal hook?

Relatively easy to achieve. We can just introduce a new state there to track scroll position, add event listeners in useEffect hook and pass ref to the BaseModal to get the content element to attach the listeners to. Something like this:

1export const ModalBase = React.forwardRef(({ isOpen, onClosed }: ModalProps, ref: RefObject<any>) => {
2 return isOpen ? (
3 <>
4 <div css={modalBlanketCss} onClick={onClosed} />
5 <div css={modalBodyCss} ref={ref}>
6 // add a lot of content here
7 </div>
8 </>
9 ) : null;
12export const useModal = () => {
13 const [isOpen, setIsOpen] = useState(false);
14 const ref = useRef<HTMLElement>(null);
15 const [scroll, setScroll] = useState(0);
17 // same as before
19 useEffect(() => {
20 const element = ref.current;
21 if (!element) return;
23 const handleScroll = () => {
24 setScroll(element?.scrollTop || 0);
25 };
27 element.addEventListener('scroll', handleScroll);
28 return () => {
29 element.removeEventListener('scroll', handleScroll);
30 };
31 });
33 const Dialog = () => <ModalBase onClosed={close} isOpen={isOpen} ref={ref} />;
35 return {
36 isOpen,
37 Dialog,
38 open,
39 close,
40 };

And now we can do whatever with this state. Now let’s pretend that the previous performance problems are not that big of a deal, and use again this hook directly in the slow Page component. See codesandbox.

The scrolling doesn’t even work properly! 😱 Every time I try to scroll the dialog content it resets to the top!

Okay, let’s think logically. We know already, that creating components inside render functions is evil, since React will re-create and re-mount them on every re-render. And we know that hooks change with every state change. That means now, when we introduced scroll state, on every scroll change we’re changing state, which causes the hook to re-render, which causes Dialog component to re-create itself. Exactly the same problem, as with creating components inside render functions, with exactly the same fix: we need to extract this component outside of the hook or just memoise it.

1const Dialog = useMemo(() => {
2 return () => <ModalBase onClosed={close} isOpen={isOpen} ref={ref} />;
3}, [isOpen]);

The focus behaviour is fixed, but there is another problem here: the slow Page component re-renders on every scroll! That one is a bit hard to notice since the dialog content is just text. Try, for example, to reduce the CPU by 6x, scroll, and then just highlight the text in the dialog right away. The browser won’t even allow that, since it’s too busy with re-renders of the underneath Page component! See the codesandbox. And after a few scrolls, your laptop will probably try to take off to the Moon due to 100% CPU load 😅

Yeah, we definitely need to fix that before releasing it to production. Let’s take another look at our component, especially at this part:

1return {
2 isOpen,
3 Dialog,
4 open,
5 close,

We’re returning a new object on every re-render, and since we re-render our hook on every scroll now, that means that object changes on every scroll as well. But we’re not using the scroll state here, it’s entirely internal for the useModal hook. Surely just memoising that object will solve the problem?

1return useMemo(
2 () => ({
3 isOpen,
4 Dialog,
5 open,
6 close,
7 }),
8 [isOpen, Dialog],

You know the best (or the scariest) part of this? IT DIDN’T! 😱 See the codesandbox.

And this is another huge performance-related bummer with hooks. Turns out, it doesn’t really matter, whether the state change in hooks is “internal” or not. Every state change in a hook, whether it affects its return value or not, will cause the “host” component to re-render.

And of course exactly the same story with chaining hooks: if a hook’s state changes, it will cause its “host” hook change as well, which will propagate up through the whole chain of hooks until it reaches the “host” component and re-renders it (which will cause another chain reaction of re-renders, only downstream now), regardless of any memoisation applied in between.

Extracting the “scrolling” functionality into a hook will make absolutely no difference, the slow Page component will re-render 😔.

1const useScroll = (ref: RefObject) => {
2 const [scroll, setScroll] = useState(0);
4 useEffect(() => {
5 const element = ref.current;
6 if (!element) return;
8 const handleScroll = () => {
9 setScroll(element?.scrollTop || 0);
10 };
12 element.addEventListener('scroll', handleScroll);
13 return () => {
14 element.removeEventListener('scroll', handleScroll);
15 };
16 });
18 return scroll;
21export const useModal = () => {
22 const [isOpen, setIsOpen] = useState(false);
23 const ref = useRef<HTMLElement>(null);
24 const scroll = useScroll(ref);
26 const open = useCallback(() => {
27 setIsOpen(true);
28 }, []);
30 const close = useCallback(() => {
31 setIsOpen(false);
32 }, []);
34 const Dialog = useMemo(() => {
35 return () => <ModalBase onClosed={close} isOpen={isOpen} ref={ref} />;
36 }, [isOpen, close]);
38 return useMemo(
39 () => ({
40 isOpen,
41 Dialog,
42 open,
43 close,
44 }),
45 [isOpen, Dialog, open, close],
46 );
See the codesandbox.

How to fix it? Well, the only thing to do here is to move the scroll tracking hook outside of the useModal hook and use it somewhere where it won’t cause the chain of re-renders. Can introduce ModalBaseWithAnalytics component for example:

1const ModalBaseWithAnalytics = (props: ModalProps) => {
2 const ref = useRef<HTMLElement>(null);
3 const scroll = useScroll(ref);
5 console.log(scroll);
7 return <ModalBase {...props} ref={ref} />;

And then use it in the useModal hook instead of the ModalBase:

1export const useModal = () => {
2 // the rest is the same as in the original useModal hook
4 const Dialog = useMemo(() => {
5 return () => <ModalBaseWithAnalytics onClosed={close} isOpen={isOpen} ref={ref} />;
6 }, [isOpen, close]);
8 return useMemo(
9 () => ({
10 isOpen,
11 Dialog,
12 open,
13 close,
14 }),
15 [isOpen, Dialog, open, close],
16 );

Now the state changes due to the scrolling will be scoped to the ModalBaseWithAnalytics component and won’t affect the slow Page component. See the codesandbox.

That is all for today! Hope this article scared you enough helped you to feel more comfortable with custom hooks and how to write and use them without compromising the performance of your apps. Let’s recap the rules of performant hooks before leaving:

  • every state change in a hook will cause its “host” component to re-render, regardless of whether this state is returned in the hook value and memoised or not
  • the same with chained hooks, every state change in a hook will cause all “parent” hooks to change until it reaches the “host” component, which again will trigger the re-render

And the things to watch out for, when writing or using custom hooks:

  • when using a custom hook, make sure that the state that this hook encapsulates is not used on the level it wouldn’t have been used with the components approach. Move it “down” to a smaller component if necessary
  • never implement “independent” state in a hook or use hooks with the independent state
  • when using a custom hook, make sure it doesn’t perform some independent state operations, that are not exposed in its return value
  • when using a custom hook, make sure that all hooks that it uses also follow the rules from the above

Stay safe and may your apps be blazing fast from now on! ✌🏼

Share on:Share "Why custom react hooks could destroy your app performance" on TwitterShare "Why custom react hooks could destroy your app performance" on LinkedInShare "Why custom react hooks could destroy your app performance" on Reddit
Get the latest content by email
    No spam or ads. Unsubscribe at any time.
    © Developer Way 2021