Super Apps: React Optimizations Tools that are Nice to Use
Tags: webdev, javascript, react, productivity
Published: 2023-04-04 15:21:02

One common oversight many beginner developers who are using React JS for frontend development may not be aware of is understanding how to optimize their React Apps for increased performance.

In this article, I’m going to discuss some handy React tools that will help optimize your app's performance.

First of all, what is web optimization? As the name implies, it’s a way of optimizing or improving the performance of your website/application. When building React apps, it’s a very good habit to use the React Developer Tools extension on your browser to monitor or track your application’s lifecycle. I could further explain how this extension works, but will very much leave it for a future article. What else can be used to optimize your React app performance? Let’s look at some tools that can help optimize your app's performance, and when and why to use them.

**1.	React.memo()**
The react.memo() is a higher-order component (HOC) used to memoize a functional component. That is, it takes in a component and returns a memoized version of the component. 

Code example:
```
import { memo } from 'react';

function MyComponent = memo((props) { 

// Component logic

 }) 

export default MyComponent;
```
As seen in the above example, the memo function takes the component and would only re-render the component when the props value provided to the component changes. 
Let’s say you have a child component in your react app that re-renders unnecessarily, to prevent such and allow for re-render only when the props change…React.memo is your guy for the job. 

React.memo takes in a component and returns a cached/memoized version of the component, which will only re-render if the props passed to the component are not the same as the previous or cached one. Although React.memo is a nice-to-use tool, it does have its drawbacks, two especially to be aware of are:

i.	Increased memory usage: While React.memo can improve your app's performance, it can also be a burden in your application. For instance, React.memo creates a memoized version of your component whenever the props of the component change, and this can be detrimental especially if the component manages a large number of props which will require re-render anytime any of the props changes potentially resulting in increased memory usage.

ii.	Limited performance benefits: React.memo can only improve performance in certain situations such as when a component has a large number of props. But in cases, such as when a component depends heavily on state or context, React.memo may not provide much of a performance benefit.

**2.	useMemo()**
React useMemo is a hook provided by the React library that allows you to memoize expensive function calls and avoid unnecessary re-renders in your application.
Here's an example of how to use useMemo:

Code example:

```
import React, { useMemo } from 'react';

function MyComponent({ data }) { 

const expensiveFunction = useMemo(() => { 

// some expensive calculations based on `data` return result;
 }, [data]); 

// use the result of `expensiveFunction` in your component 
return ( 
	<div> {expensiveFunction} </div>
 );
} 
```
In the above example, expensiveFunction will only re-run if the data prop being passed as a dependency to useMemo has changed. If it hasn't changed, React will return the memoized result from the previous render, avoiding the need to run expensiveFunction again.

useMemo is used to memoize a value. It takes a function that is expected to return a value and an array of dependencies, and returns a memoized value. The function will only re-run to return a value when the dependencies have changed. This is useful for expensive calculations or complex data transformations that can be cached and reused. Here are some situations where useMemo might be used to memoize a value:

i.	A complex calculation that is computationally expensive, such as calculating the factorial of a large number, or sorting a large array.
ii.	A data transformation that involves filtering, sorting, or grouping data.
iii.	A function that makes an API call or performs an expensive operation, such as rendering an image or generating a PDF.

In all of these examples, the memoized value is only recomputed when the dependencies change, which can help improve the performance of the application by avoiding unnecessary re-renders.

**3.	useCallback()**
useCallback is a React hook that memoizes a given function instance, returning a memoized version of the function that only changes if one of its dependencies has changed.
This can be useful when you want to prevent unnecessary re-renders of child components. By memoizing a function with useCallback, you can ensure that the child components only re-render when necessary.

The useCallback hook takes two arguments: the first is the function that you want to memoize, and the second is an array of dependencies. The dependencies are used to determine whether or not to create a new version of the memoized function. If any of the dependencies change, a new memoized function will be created.
Here's an example usage of useCallback:

Code example:

```
import React, { useCallback } from 'react'; 

function MyComponent(props) { 

const { onClick } = props; // Memoize the onClick handler 

const handleClick = useCallback(() => { 

console.log('Button clicked');

 onClick(); 
}, [onClick]);

return ( 
	<button onClick={handleClick}>Click me</button> 
); 
} 
```
In this example, the handleClick function is memoized using useCallback. The function will only be recreated if the onClick dependency changes. This can improve performance by preventing unnecessary re-renders of MyComponent.
useCallback is used to memoize a function so that it can be cached and reused, preventing it from being recreated on every render. This is particularly useful when passing a function as a prop to a child component, as it ensures that the child component only re-renders when necessary. Note that functions passed to a child component will only trigger a re-render in the child component if the reference to that function changes in the parent component since functions are reference object. Re-render or re-evaluation of a component will create a new reference to a function.

In conclusion, both `useCallback` and `useMemo` are React hooks that are used to optimize the performance of a React application by preventing unnecessary re-rendering of components. This tools and more can serve a great deal if you get to know them better, like you would know a friend before you commit to them.

I am [Jude Ebeke](https://www.linkedin.com/in/jude-ebeke-b67813223/)  and you can reach me through my [email.](judeebekes67@gmail.com)