简体   繁体   中英

Accessing `props` out of constructor

I simply made a state object since there was no need of constructor. I accessed props in that state object as this.props , so here in this state object I am able to access the props using this keyword. Can someone state any reason for not using it the current way and defining a constructor. state={visibility: this.props.visibility}

got following result from babel.repl

https://babeljs.io/repl/#?babili=false&browsers=&build=&builtIns=false&code_lz=MYGwhgzhAEbQpgDwC7wHYBMYAUCuAneAYQHsBbABxLXWWgG8AoR6aCZMVaAXgZdbYdkuCAC5o2AIIAlSQFkAygG0A5vGTtOIgBTIAFgEsIAOgr4SFE2XhQwagJQBdY5uEQANPwC-Abn78AegDoYGp2fFxgZBJ8bTMLCHsmViCBCFwKeFj4y3s_FODWfSMXIXgePgLoQlCya0xOA2oFIRFxaQBRIgB5OTkOgDkAEUkAFQBJboGAfTlukYAZaalZRVV1aXha-oxG5taIOPNLY2tbB2dXEU8qgEhfQOCvRmegA&debug=false&circleciRepo=&evaluate=true&fileSize=false&lineWrap=false&presets=es2016%2Cstage-2&prettier=false&targets=&version=6.26.0

我找到了答案,如果我们在构造函数中或在类https://michalzalecki.com/react-components-and-class-properties/中进行初始化,则没有任何区别

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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