39 lines
1.2 KiB
Plaintext
Raw Normal View History

This rule reports the methods of React component classes which are neither used nor React lifecycle methods. Note that this rule checks only the scope of a component class, which means that issues will be raised on the methods that are only used outside of the class.
Methods that are never executed are dead code: unnecessary, inoperative code that 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.
React encourages data-driven components. To modify a child component, re-render it with new `props`.
== Noncompliant Code Example
[source,javascript]
----
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';
}
}
----
== Compliant Solution
[source,javascript]
----
class Profile extends React.Component {
render(props) {
return <h1>{ props.name || getDefaultName() }</h1>;
}
getDefaultName() {
return 'John Smith';
}
}
----
== See
* https://reactjs.org/docs/components-and-props.html[Components and Props] - React Documentation