archived

Pure vs functional vs class React components

The types of components in React is on of those topics that many don’t quite grasp, or assume that it isn’t worth the effort to learn it for the performance boost that they may get back. Honestly, though, the types of components in React aren’t that complicated or difficult to understand. Learning the different types will help you not only get a better grasp of what React is doing under the hood, but also help you write cleaner, more performant code.

Every React developer knows about the two “major” component types in React; class components and functional components. The majority of React code is written using just these two types. There’s another type that most developers have probably heard about, however. That is the pure component. The differences are subtle, but they can make a sizable difference in code clarity and potentially in the application’s performance.

Functional Components

The first type of component, and also the simplest, is the functional component. As it’s name suggests, it is a function. There’s nothing special about functional components, and that’s what makes them so simple to use. Whatever the function returns is what React will render. These components are great for displaying things, because they are pure. That is, given the same props, they will return the same thing. This makes them easy to test, and great to use because there aren’t any side effects. There are some features that functional components lack, and that’s when a developer can turn to the functional component’s big brother, the class component.

Class Components

Class components can be similar to a functional component. If a class component only has a render method, it performs neraly the same as a functional component. That said, one would typically use a a functional component in that case. The main reason one would use a class component is for the additional features it provides over a functional component, such as state, refs, and lifecycle methods. For more information on those, feel free to look at their respective links. The main part we will focus on here is the lifecycle methods. Lifecycle methods provide ways to “hook” into different events that happen to a component. These allow you to run functions when the component mounts/unmounts, recieves new props, updates, and more. Additionally, some lifecycle methods provide ways to bypass or prevent certain events from happening. The most notable in this category is the shouldComponentUpdate (SCU) method. By default, any time that a component’s state or props change, the component will rerender. Do note that just because a component re-renders doesn’t mean the DOM will get updated (See here for more details). There are times however, when a component shouldn’t rerender when the props or state change. This is typically most productive when you have a large component with an expensive render method. These situations are typically rather unique, and you’ll probably know when you should implement SCU. An alternative to writing a custom SCU function is using a Pure Component.

Pure Components

Pure components are very similar class components. Really, the only difference between them is that by default, class components don’t have a SCU function. Pure components, on the other hand, do implement a SCU function that does a shallow comparison of the props. That is, they don’t recursively compare into the props children. The advantage is that often times, a render can be skipped if the props are the same. As noted, this is going to have the most benefit with a component that has an expensive render method. This also provides an advantage over functional components, as they don’t have any lifecycle methods, and therefore rerender every time.

When to Use Each Type

So when should one use which method? That question is a bit complicated to answer. Some basic guidelines are as follows:

← back to writing