Movatterモバイル変換


[0]ホーム

URL:


OverriddenshouldComponentUpdate() should not always return a truthy value

  • REACT_USELESS_SHOULD_COMPONENT_UPDATE
  • Code Quality
  • Low
  • react

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

SinceshouldComponentUpdate() defaults totrue, it is useless if the overriddenshouldComponentUpdate() 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 ExampleCompliant Code Example
1import React from 'react';1import React from 'react';
22
3export class Hello extends React.Component {3export class Hello extends React.Component {
4 constructor(props) {4 constructor(props) {
5 super(props);5 super(props);
6 this.state = { greetName: 'Hi' };6 this.state = { greetName: 'Hi' };
7 this.handleClick = this.handleClick.bind(this);7 this.handleClick = this.handleClick.bind(this);
8 }8 }
9 handleClick() {9 handleClick() {
10 this.setState({ greetName: 'Bye' });10 this.setState({ greetName: 'Bye' });
11 }11 }
12 shouldComponentUpdate(nextProps, nextState) { // REACT_USELESS_SHOULD_COMPONENT_UPDATE alarm because this always returns true.12 shouldComponentUpdate(nextProps, nextState) {
13 if (nextState.greetName !== this.state.greetName) {13 if (nextState.greetName !== this.state.greetName) {
14 return true;14 return true;
15 }15 }
16 return true;16 return false;
17 }17 }
18 render() {18 render() {
19 return <div onClick={this.handleClick}>{this.state.greetName}</div>;19 return <div onClick={this.handleClick}>{this.state.greetName}</div>;
20 }20 }
21}21}

Noncompliant Code Example

View with compliant examples side by side
import React from 'react';export 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

View with noncompliant examples side by side
import React from 'react';export 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

Was this documentation helpful?

Last updated on April 29, 2025

[8]ページ先頭

©2009-2025 Movatter.jp