Unused or undefined React component state properties can cause errors or make code hard to read and understand. Any computation used to initialize an unused state property is wasted, which may lead to performance problems. Any access to an undefined component state property trivially evaluates to the value undefined, which may come as a surprise.

Remove unused component state properties. Assign values to undefined component state properties.

In the code below, the React component Clock attempts to display the current time in the render method.

But since there are no assignments to this.state.date, the render method will throw an exception when attempting to access the property toLocaleString of the value undefined. To avoid this exception, assign the date property before using it:

  • React: Component State.
  • React: State and Lifecycle.