I know that this question was asked many times, but it is still doesn't clear. Many people just said:
Pass props to constructor if you want to access
this.propsthere
one more example of the answer
Oficial doc says Class components should always call the base constructor with props. , but if we do not pass props to constructor, we will still have this.props everywhere exept constructor.
Also from react source code we can see source code of the React.Component
function ReactComponent(props, context) {
this.props = props;
this.context = context;
}
But it is confuses me even more.
super() should be called with two parametrs: props and context . But we invoked our super empty and still have access two this.props.
According to ECMA documentation super() invokes parent constructor() with parametrs passed to super() . But our super() is empty.
So my questions are:
- Why official docs says:
Class components should always call the base constructor with props.
- How React set
propsto child component ifsuper()andconstructor()is empty? - Is it bug of feature of the React that props are accessible in child component without passing props to
super()andconstructor()?
constructor(...args) { super(...args); }- zerkmscreateElementaddspropsregardless if you usesuper(props). - Andrew Liconstructoralways withprops? - mr__brainwash