DEV Community

Cover image for Strict Mode in React
Vishnu Satheesh
Vishnu Satheesh

Posted on • Edited on

Strict Mode in React

React, being a popular JavaScript library for building user interfaces, offers a powerful feature called Strict Mode to enhance development practices and catch potential issues early on. In this blog, we'll delve into the significance of Strict Mode in React development and explore how it can help improve your code quality and debugging process.

In React, Strict Mode is a development mode feature that helps you catch potential problems in your application's code and encourages best practices. It performs a series of checks and warnings during rendering and helps you identify and fix issues in your code that might lead to bugs or unexpected behavior in production.

Strict Mode provides the following benefits:

  • Identifying Unsafe Practices: It helps identify and warn about unsafe practices in your code, such as using deprecated lifecycle methods, accessing the state directly, or setting properties that are read-only.

  • Detecting Side Effects: It highlights components with unexpected side effects during rendering. This can help you catch unintentional side effects and optimize your component's rendering performance.

  • Improved Warnings: Strict Mode includes extra checks and improved warning messages for various common mistakes and potential issues, making it easier to spot and fix problems early in the development process.

To enable Strict Mode in a React application, you can wrap your entire application or specific components in the <React.StrictMode> component. Here's an example of how to use it:



import React from 'react';
import ReactDOM from 'react-dom';

ReactDom.render(
 <React.StrictMode>
  <App/>
 </React.StrictMode>,
 document.getElementById('root')
);


Enter fullscreen mode Exit fullscreen mode

Now, let's create a StrictComponent to illustrate some of the benefits of Strict Mode:



import React, { useState } from 'react';

const StrictComponent = () => {
  const [count, setCount] = useState(0);

  // Simulate a side effect inside the render method
  console.log('Rendered!')

  // Attempt to modify state directly (should trigger a warning)
  const incrementDirectly = () => {
    count++;
    console.log(`Count: ${count}`);
  };

  return ( 
    <div>
      <h1>Strict Component</h1>
      <p>Count: {count} </p>
      <button onClick={incrementDirectly}>Increment Directly</button>
      <button onClick={() => setCount(count + 1)}>Increment Properly </button>
    </div>
  )
};

export default StrictComponent;


Enter fullscreen mode Exit fullscreen mode

In this example, we have a component called StrictComponent that contains a state variable count.
Inside the render method, we have a console.log statement, simulating a side effect. Additionally, there's a button that attempts to modify the count state directly, which is an unsafe practice.

You can also enables Strict Mode for any part of your application:



import { StrictMode } from 'react';

function App() {
  return (
    <>
     <Header />
     <StrictMode>
      <main> 
        <Sidebar />
        <Content />
      </main>
     </StrictMode>
    </>
  )
}


Enter fullscreen mode Exit fullscreen mode

When you render the StrictComponent within a <React.StrictMode> wrapper, React will perform extra checks and display warnings for the unsafe practices and side effects.
You will see warnings in the browser's console indicating that you should not modify state directly and that there's a side effect during rendering.

By using Strict Mode during development, you can catch and address these issues early, leading to a more reliable and optimized React application. Once you've fixed the problems identified by Strict Mode, your application will be more robust and ready for production.

Stay tuned for more insights and practical tipsπŸš€

Buy Me A Coffee

Top comments (2)

Collapse
 
joes_bffac7a634ab profile image
Joes

Image description

Collapse
 
vishnusatheesh profile image
Vishnu Satheesh

Thank you for your feedback! I’d love to hear more about what you found unclear or could be improved so I can make the content better for everyone.