在没有构造函数的情况下反应定义状态 [英] React defining state without a constructor
本文介绍了在没有构造函数的情况下反应定义状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到一些反应开发人员在没有构造函数的情况下定义状态.我喜欢简化代码的事实,但是这样做安全吗?
I've seen some react developers define state without a constructor. I like the fact that is simplifies the code, but is it safe to do so?
class Dog extends React.Component {
state = { sound: 'Woof' }
return (
<p>Dog says { this.state.sound }</p>
)
}
对于这个过度简化的示例,我事先表示歉意.
I apologize in advance for the over simplified example.
推荐答案
完全相同.看一下babel将代码转换成的javascript.没有区别.
Its exactly the same thing. Take a look at the javascript that babel transpiles the code into. There is no difference.
这篇关于在没有构造函数的情况下反应定义状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文