DEV Community

Cover image for Understanding the Impact of React.StrictMode on Application Performance
Glover
Glover

Posted on

1

Understanding the Impact of React.StrictMode on Application Performance

React.StrictMode is a tool introduced in React 16.3 that helps identify potential problems in an application. It activates additional checks and warnings for components within its scope, which helps improve the performance and correctness of an application.

One of the key features of React.StrictMode is its ability to detect unexpected side effects in components. In React, side effects refer to any operations that mutate state or trigger other changes outside the component, such as network requests or manually modifying the DOM. By default, these side effects are allowed to happen asynchronously, which can lead to unexpected behavior and performance issues.

React.StrictMode wraps its children in a fragile and isolated environment, where any side effects are detected and reported. This allows developers to find and fix potential problems before they become severe. For example, consider the following component that triggers a side effect by updating the document title:

function MyComponent() {
  React.useEffect(() => {
    document.title = 'My page';
  });

  return <h1>Hello world!</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Without React.StrictMode, this component would silently update the document title without any warnings. However, when wrapped in React.StrictMode, it would produce a warning in the console:

// MyComponent.js:5
//   React has detected a legacy lifecycle event in a strict-mode component.
//   As a precaution, React will freeze this component...
Enter fullscreen mode Exit fullscreen mode

This warning indicates that the useEffect hook, which is used to manage side effects, has been called in a strict mode component. In the future, this hook will be frozen and can no longer be used, which means that the component will need to be updated to avoid the side effect.

In addition to detecting side effects, React.StrictMode also enables additional checks and warnings for other potential problems. For instance, it can detect deprecated APIs and component patterns that will be removed in future versions of React. It can also help improve the performance of an application by highlighting unnecessary rendering and outdated styles.

Here is an example of how React.StrictMode can detect deprecated APIs:

function MyComponent() {
  React.useLegacyContextAPI(MyContext);

  return <h1>Hello world!</h1>;
}

// MyComponent.js:3
//   React has detected a legacy context API being used...
Enter fullscreen mode Exit fullscreen mode

In this example, the useLegacyContextAPI hook is deprecated and will be removed in a future version of React. When used in a strict mode component, it produces a warning that advises developers to update their code to the new context API.

Strict mode checks are run in development mode only; they do not impact the production build.

Overall, React.StrictMode is a valuable tool for improving the performance and correctness of a React application. By detecting unexpected side effects and other potential problems, it helps developers catch and fix issues before they become severe. While it may require some additional work to update components to avoid the warnings it produces, the long-term benefits of using React.StrictMode are well worth the effort.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay