"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Do Multiple Renders Occur with useState in Non-Strict Mode?

Why Do Multiple Renders Occur with useState in Non-Strict Mode?

Published on 2024-11-04
Browse:650

Why Do Multiple Renders Occur with useState in Non-Strict Mode?

Why Multiple Renders with useState in Non-Strict Mode?

When using useState in React, you might notice your component rendering more than once. This behavior, which can appear even without enabling strict mode, requires some clarification.

Upon clicking the "Change number" button, you'll observe two console statements ("here") due to an extra component render. The reason behind this is not strict mode, as previously suggested. Instead, React triggers a double render to facilitate a more deterministic development environment.

According to React's documentation, non-strict mode in development does the following:

  • Intentionally invokes component constructor, render, and state updater functions twice.
  • Doubles the invocation of function components' bodies and hooks like useState.

This double rendering provides an opportunity to detect potential side effects in your code, highlighting performance issues or bugs early on. While this extra rendering doesn't occur in production, it serves as a valuable troubleshooting aid during development.

Release Statement This article is reprinted at: 1729694957 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3