简体   繁体   English

React 究竟是如何让 this.props 在类组件中可用的

[英]How exactly does React make this.props available in a class Component

If you want to use this.props in the constructor, you need to pass props to super(). Otherwise, it doesn't matter because React sets .props on the instance from outside immediately after calling the constructor.

所以我很好奇,如果构造函数缺少 super(props) 或者根本没有构造函数,React 如何在调用构造函数后立即在基于类的 Component 中设置 .props 。

如果构造函数缺少super(props) (它使用给定的参数调用父构造函数),则不会设置this.props ,但是如果缺少整个构造函数,则将调用父构造函数,因为在制作基于类的组件时,您必须从React.Component扩展。

如果您不在构造函数中调用 super(props) ,您将不会获得道具......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM