反应中的 StrictMode 是什么? [英] What is StrictMode in react?

查看:37
本文介绍了反应中的 StrictMode 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说严格模式通过抛出生命周期方法移除警告来帮助以最佳实践方式编写 React 代码.

I heard that strict mode helps to write React code in best practices way by throwing warnings for life cycle methods removal.

我从 https://medium 读到了它.com/@baphemot/whats-new-in-react-16-3-d2c9b7b6193b

我的理解正确吗?严格模式有多有效?是不是只针对不安全的生命周期方法?如果没有,我可以在功能组件中使用此功能吗?

Is my understanding correct? How effective is strict mode? Is it only for unsafe life cycle methods? If not can I use this feature in functional components?

  import { StrictMode} from "react";
  class Test extends Component{
        render(
            <StrictMode>
                   //Some other child component which has all lifecycle methods implemented in it
            </StrictMode>
        );
  }

推荐答案

React 的 StrictMode是一种辅助组件,可以帮助您编写更好的 React 组件,您可以使用 <StrictMode/> 包装一组组件,它基本上会:

React's StrictMode is sort of a helper component that will help you write better react components, you can wrap a set of components with <StrictMode /> and it'll basically:

  • 验证内部组件是否遵循一些推荐的做法,如果不在控制台中,则警告您.
  • 确认没有使用已弃用的方法,如果使用了它们,则严格模式会在控制台中向您发出警告.
  • 通过识别潜在风险帮助您预防一些副作用.
  • Verify that the components inside are following some of the recommended practices and warn you if not in the console.
  • Verify the deprecated methods are not being used, and if they're used strict mode will warn you in the console.
  • Help you prevent some side effects by identifying potential risks.

正如文档所说,严格模式是面向开发的,因此您无需担心它会影响您的生产构建.

As the documentation says, strict mode is development oriented so you don't need to worry about it impacting on your production build.

我发现当我在处理新代码库并且想查看我所面对的代码/组件类型时,实施严格模式特别有用.此外,如果您处于 bug 搜寻模式,有时最好用 包装您认为可能是问题根源的代码组件/块.

I've found it especially useful to implement strict mode when I'm working on new code bases and I want to see what kind of code/components I'm facing. Also if you're on bug hunting mode, sometimes it's a good idea to wrap with <StrictMode /> the components/blocks of code you think might be the source of the problem.

所以,是的,您正走在理解严格模式的正确道路上,坚持下去,我认为这是您在使用它们时会更好地理解的事情之一,所以继续玩乐吧.

So yeah, you're in the correct path to understanding strict mode, keep it up, I think it's one of those things you understand better when you play with them, so go ahead and have some fun.

这篇关于反应中的 StrictMode 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆