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:

  1. this.props is accessed in the class constructor before the super(props) call.
  2. The props argument is not passed when calling super() in the class constructor.

Noncompliant Code Example

View with compliant examples side by side
import 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 side
import 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.

Was this documentation helpful?