Overridden shouldComponentUpdate() should not always return a truthy value

  • REACT_USELESS_SHOULD_COMPONENT_UPDATE
  • Code Quality
  • Low
  • react

This rule applies when an overridden shouldComponentUpdate() method always returns a truthy value.

Since shouldComponentUpdate() defaults to true, it is useless if the overridden shouldComponentUpdate() method always returns a truthy value.

In this case, shouldComponentUpdate() method's logic is probably wrong by a developer's mistake or misunderstanding of the method.

Noncompliant Code Example

import React from 'react';

class Hello extends React.Component {
    constructor(props) {
        super(props);
        this.state = { greetName: 'Hi' };
        this.handleClick = this.handleClick.bind(this);
    }
    handleClick() {
        this.setState({ greetName: 'Bye' });
    }
    shouldComponentUpdate(nextProps, nextState) { // REACT_USELESS_SHOULD_COMPONENT_UPDATE alarm because this always returns true.
        if (nextState.greetName !== this.state.greetName) {
            return true;
        }
        return true;
    }
    render() {
        return <div onClick={this.handleClick}>{this.state.greetName}</div>;
    }
}

Compliant Code Example

import React from 'react';

class Hello extends React.Component {
    constructor(props) {
        super(props);
        this.state = { greetName: 'Hi' };
        this.handleClick = this.handleClick.bind(this);
    }
    handleClick() {
        this.setState({ greetName: 'Bye' });
    }
    shouldComponentUpdate(nextProps, nextState) {
        if (nextState.greetName !== this.state.greetName) {
            return true;
        }
        return false;
    }
    render() {
        return <div onClick={this.handleClick}>{this.state.greetName}</div>;
    }
}

Version

This rule was introduced in DeepScan 1.4.0-beta.

See