有没有办法检查React' s“ strictmode”是否已打开?

发布于 2025-02-05 04:34:41 字数 889 浏览 3 评论 0原文

在代码中或通过React DevTools中有一种方法,以查看

需要明确的是,这个问题是:

  • 不是关于如何启用严格模式的。 (答案是< react.strictmode>,尽管也可以通过某些框架中的配置设置它,例如,next.js
  • 专门讨论了React stricts strictmode在/a>('使用strart';)或 typescript> typescript的严格模式。这是一个超载的短语,但是Watcha会这样做吗?
  • 由于与React的Strictmode意外渲染的意外渲染,您的混乱动机。请参阅 this github问题此stackoverflow post 为了使其造成挫败感。即使您无法启用了“ strictmode”中的何处,也可以使用一种简单的方法来验证组件正在严格模式中运行。

Is there a way in React -- either in code or via the React devtools -- to see if a component is being rendered in React's StrictMode? In my dream world, there would be some kind of constant I could console.log, say a bool of React.isStrictModeOnForThisComponentRendering; or maybe when you inspect a component in the React devtools, it would say somewhere in the side panel if it's rendering in StrictMode or not.

To be clear, this question is:

  • Not about how do you enable StrictMode. (The answer is <React.StrictMode>, although it can also be set via configs in some frameworks, e.g., Next.js)
  • Specifically discussing React's StrictMode, not JavaScript's ('use strict';) or TypeScript's strict mode. It's an overloaded phrase, but watcha gonna do?
  • Motivated by the confusion you get due to unexpected double rendering with React's StrictMode. See this GitHub issue or this StackOverflow post for a sense of the frustration it can cause. It would be nice to have an easy way to verify a component is running in StrictMode even if you can't tell where in the component tree StrictMode has been enabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2025-02-12 04:34:41

这不是一种清晰或确定的方式,但是您可以有目的地触发仅在React的Strictmode中发生的警告。例如:

  • 您可以有目的地创建一个使用不安全生命周期的虚拟组件,它应该触发警告。这有点麻烦,因为 - 如果您在应用程序中仅使用功能组件,则必须创建一个类组件来使用不安全的生命周期方法。
  • 您可以在组件中有目的地使用(不推荐使用的)字符串ref,它应该触发警告。这比上面的功能组件更容易但丑陋:您可以将其放入任何组件的JSX中,但是如果它是功能性的组件,它将使您的应用完全断开。但是警告至少应该在应用程序中断之前出现。 警告:我刚刚尝试复制它,尽管我仍然相信我的应用程序在React strictmode中,但我再也无法像以前看到的那样触发字符串ref触发警告,但它只是唯一的打破应用程序。不确定发生了什么变化或我做不同的事情,但这可能不像上述选项那样好。

无论如何,此警告将包含一些类似的文本,

... found within a strict-mode tree ...

这些文本似乎是相当不错的证明,证明该组件正在strictmode中呈现。拥有更直接的辅助功能会很好,但是它可以捏合。

It's not a clear or definitive way, but you can purposefully trigger a warning that is supposed to only occur in React's StrictMode. For example:

  • You could purposefully create a dummy component that makes use of an unsafe lifecycle, and it should trigger a warning. This is a bit of a hassle since -- if you're using only functional components throughout your app -- you must create a class component to make use of the unsafe lifecycle methods.
  • You could purposefully use a (deprecated) string ref in a component, and it should trigger a warning. This is easier but uglier than the above with functional components: you can put it in any component's JSX, but it will cause your app to just completely break if it's a functional component. But the warning should at least still appear before the app breaks. CAVEAT: I just tried reproducing this and while I'm still confident my app is in React StrictMode, I can no longer get the string ref to trigger the warning as I had been seeing it before, it's only breaking the app. Not sure what has changed or I'm doing differently, but this might not be as good a check as the above option.

In any case, this warning will contain some text like this

... found within a strict-mode tree ...

which seems like reasonably good proof that the component is rendering in StrictMode. Would be nice to have a more direct helper function, but it works in a pinch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文