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

查看:97
本文介绍了什么是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>
        );
  }

推荐答案

反应的StrictMode 是一种 helper组件,可以帮助您编写更好的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:

  • 验证内部组件是否遵循推荐做法的 some ,如果不在控制台中,则会发出警告.
  • 验证未使用的方法没有被使用,如果使用了严格模式,则会在控制台中警告您.
  • 通过识别潜在风险来帮助您预防某些副作用.
  • 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.

当我在开发新的代码库时,我想实现 strict模式尤其有用,我想看看我要面对的是哪种代码/组件.另外,如果您处于错误寻找模式,有时最好用<StrictMode />包装您认为可能是问题根源的组件/代码块.

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天全站免登陆