I am taking a beginner's course on react js. I came across class components and I wanted to know how does the props object get initialized.
for eg:
Main Component:
class App extends React.Component {
render() {
return (
<div>
<Header username="xyz"/>
</div>
)
}
}
Header:
class Header extends React.Component {
render() {
return (
<div>
<p>Welcome, {this.props.username}</p>
</div>
)
}
}
I wanted to know does the props object in Header class is getting initialized?
Shouldn't it be something like this:
class Header extends React.Component {
constructor(props) {
super()
this.props = props
}
render() {
return (
<div>
<p>Welcome, {this.props.username}</p>
</div>
)
}
}
By using the above code I am a getting a message:
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
My question is, if we want to pass anything to a class, shouldn't it be done using a constructor?