super() OR super(props) -- What's The Difference
来源:互联网 发布:易经软件 知乎 编辑:程序博客网 时间:2024/06/03 21:23
Questions:
When is it important to passprops
tosuper()
, and why ?
class MyComponent extends React.Component { constructor(props) { super(); // or super(props) ? }}
Answer :
There is only one reason when one needs to pass props
to super()
:
When you want to access this.props
in constructor.
Passing:
class MyComponent extends React.Component { constructor(props) { super(props) console.log(this.props) // -> { icon: 'home', … } }}
Not passing:
class MyComponent extends React.Component { constructor(props) { super() console.log(this.props) // -> undefined // Props parameter is still available console.log(props) // -> { icon: 'home', … } } render() { // No difference outside constructor console.log(this.props) // -> { icon: 'home', … } }}
Note that passing or not passing props to super has no effect on later uses of this.props outside constructor. That is render, shouldComponentUpdate, or event handlers alwayshave access to it.
Ben Alpert from React team:
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 the outside immediately after calling the constructor.
阅读全文
0 0
- Android动画之layoutAnimation与gridLayoutAnimation
- 训练的神经网络不工作?一文带你跨过这37个坑
- SOAP webserivce 和 RESTful webservice 对比及区别
- 客户端到服务器端通信过程及原理
- 20非常有用的JAVA程序片段
- super() OR super(props) -- What's The Difference
- Dropwizard与Spring Boot比较
- Linux下利用phpize安装memcashe的php源码扩展包
- 需求文档到底谁来写
- Java动态代理的两种实现方法
- java运行时数据区划分
- tensorflow 控制流操作,条件判断和循环操作
- 详解this指针--为什么空的对象指针可以调用成员函数
- QT HTTP 服务 代理服务器没有响应