Gaurav Soni
Gaurav Soni

Reputation: 411

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

Upvotes: 0

Views: 34

Answers (1)

Gaurav Soni
Gaurav Soni

Reputation: 411

I found the answer in, it makes no difference if we initialise in constructor or in class https://michalzalecki.com/react-components-and-class-properties/

Upvotes: 1

Related Questions