Home > Web Front-end > JS Tutorial > Why Do Strict Mode and useState Cause Multiple Component Renders?

Why Do Strict Mode and useState Cause Multiple Component Renders?

Patricia Arquette
Release: 2024-10-24 03:24:31
Original
1091 people have browsed it

Why Do Strict Mode and useState Cause Multiple Component Renders?

Why Does useState Cause the Component to Render Multiple Times During an Update?

In the provided code snippet, the App component is wrapped in React.StrictMode, which enables strict mode. In this mode, React intentionally double-invokes various functions, including:

  • Function component bodies
  • Functions passed to useState, useMemo, or useReducer

In your case, the chaneNumber function is double-invoked due to the use of useState. Each invocation of this function triggers a state update, causing the component to re-render.

Solution:

Avoid wrapping the App component in React.StrictMode. This will prevent the double-invocation of the state-changing function, resulting in the component rendering only once during an update.

The above is the detailed content of Why Do Strict Mode and useState Cause Multiple Component Renders?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template