Uninitialized this.props
of a React class component should not be accessed
- REACT_UNINITIALIZED_PROPS
- Error
- High
- react
This rule applies when uninitialized this.props
is accessed in a React class component.
this.props
is uninitialized in the below cases and accessing its properties causes a TypeError
exception:
this.props
is accessed in the class constructor before thesuper(props)
call.- The
props
argument is not passed when callingsuper()
in the class constructor.
Noncompliant Code Example
View with compliant examples side by sideimport React from 'react';
export class Foo extends React.Component {
constructor(props) {
super();
this.state = { x: this.props.x }; // REACT_UNINITIALIZED_PROPS alarm because 'super()' is not called with 'props'.
}
render() {
return <div>{this.state.x}</div>;
}
}
Compliant Code Example
View with noncompliant examples side by sideimport React from 'react';
export class Foo extends React.Component {
constructor(props) {
super(props);
this.state = { x: this.props.x };
}
render() {
return <div>{this.state.x}</div>;
}
}
Version
This rule was introduced in DeepScan 1.5.0-beta.