Methods that are never executed are dead code and should be removed. Cleaning out dead code decreases the size of the maintained codebase, making
it easier to understand the program and preventing bugs from being introduced.
When using React class components, all non-React lifecycle methods should be called within the scope of the component. If a method is only called
from outside the class, consider using props
to interact with the component and re-render if needed, as React encourages data-driven
components.
class Profile extends React.Component {
render(props) {
return <h1>{ props.name }</h1>;
}
getDefaultName() { // Noncompliant: this method is never used and is a dead code
return 'John Smith';
}
}
To fix the issue, remove the dead code or call the method from within the component scope.
class Profile extends React.Component {
render(props) {
return <h1>{ props.name || getDefaultName() }</h1>;
}
getDefaultName() {
return 'John Smith';
}
}