Sitemap
Better Programming

Advice for programmers.

Follow publication

Member-only story

How To Write Better Functional Components in React

4 min readJan 11, 2021
Code on a laptop
Picture by Arnold Francisca on Unsplash.

Those of us who have worked with functional components in React know that the introduction of Hooks has made our lives a lot easier. However, functional components have their own set of complexities and gotchas. As a result, it can sometimes be difficult to write readable and optimized functional components. Today, we will look at five simple tips to help us do just that.

1. Memoize Complex Data

Let’s take a look at the following React component called SortedListView:

This component takes an array of items, sorts them according to the comparison function provided, and displays them. However, this sorting operation could take a large amount of time if the number of items is large or the comparison function is complicated. This could end up becoming a bottleneck because the component will re-sort the items on each re-render even if the items array or the comparisonFunc does not change, but some other prop or the state changes.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Bikash Paneru
Bikash Paneru

Written by Bikash Paneru

Technology for Passion, Noodles for Hair and Junk for Food. https://mrdivinemaniac.github.io/

Responses (4)

Write a response