Intro
Discover the art of efficiently detecting and managing initial user interactions in your web app with the potent useOnInteraction hook.
First interaction use cases
Sometimes, you need to detect the user's first interaction in the application and perform specific actions. This could involve rendering a widget, logging an entry in the database via a dedicated API call, or other tasks. Performing these operations immediately after the application mounts - during the first render in React - may slow down your Lighthouse benchmark. If you prioritize performance, it's advisable to consider utilizing this mechanism.
For example, consider a scenario where a user lands on your page that requires fetching data to display a pop-up widget. Without this mechanism, you would make a call every time the user refreshes the page, even if they don't need this data/widget. By implementing this mechanism, you load the required data only when the user truly "interacts" with the app, reducing costs and improving application performance metrics. This is possible because the additional code for the required operation can be lazy-loaded.
In the https://4markdown.com/ app, we utilize it to load the Firebase library, which is relatively large, only after the user interacts with the app. This enhancement has boosted our benchmarks from 94% to 100%. Check out the GIF below to understand how it works:
Loading
Use case
1. Detecting user interaction
Detecting the first user interaction varies between mobile and desktop devices. On mobile devices, it involves the use of a touch event, while on desktop devices, it relies on a mouse move event. Therefore, we require two distinct events and a mechanism to differentiate between them after detecting the initial user interaction.
Loading
2. Design of useOnInteraction hook
To avoid cluttering our component code with repetitive logic for detecting the first user interaction, we'll create a custom hook called useOnInteraction. Components will then simply consume this hook and execute the necessary operations based on the results.
Loading
3. Implementing useOnInteraction hook
The implementation of the useOnInteraction hook involves utilizing useState to trigger a rerender when an interaction is detected, and the useEffect hook to listen to relevant events.
Loading
When interaction is detected, we remove the current listeners and skip adding new ones with a simple if statement at the beginning of the useEffect implementation.
Summary
Now, you can listen for the first interaction and perform the necessary operations. I use this hook for lazy loading dedicated components or executing required calls/checks. For instance, it might involve checking the authorization status only after the user interacts with the app, preventing unnecessary calls to my API.
- 1. Rendering
16 minutes
Creating portals with custom usePortal hook
2 m
We will change the screens with useStepper hook
3 m
Manage components appearance with useToggle hook
4 m
Removing server warnings for useLayoutEffect with custom hook
3 m
First interaction detection with useOnInteraction hook
4 m
- 2. Forms
4 minutes
- 3. Events
26 minutes
Read the scroll metadata and direction with useScroll hook
5 m
Using clipboard with useClipboard hook
4 m
Detect outside click with the useClickOutside hook
6 m
Deep dive into useIntersectionObserver hook
4 m
Element size measurement with useElementSize hook
7 m
- 4. Guards
5 minutes
- 5. Interactions
5 minutes
Comments
Add your honest opinion about this article and help us improve the content.