@@ -492,15 +492,15 @@ eslintTester.run('no-unused-state', rule, {
492
492
parser : 'babel-eslint'
493
493
} ,
494
494
{
495
- code : `class ESLintExample extends Component {
495
+ code : `class GetDerivedStateFromPropsTest extends Component {
496
496
constructor(props) {
497
497
super(props);
498
498
this.state = {
499
499
id: 123,
500
500
};
501
501
}
502
- static getDerivedStateFromProps(nextProps, prevState ) {
503
- if (prevState .id === nextProps.id) {
502
+ static getDerivedStateFromProps(nextProps, otherState ) {
503
+ if (otherState .id === nextProps.id) {
504
504
return {
505
505
selected: true,
506
506
};
@@ -516,7 +516,29 @@ eslintTester.run('no-unused-state', rule, {
516
516
parser : 'babel-eslint'
517
517
} ,
518
518
{
519
- code : `class ESLintExample extends Component {
519
+ code : `class ComponentDidUpdateTest extends Component {
520
+ constructor(props) {
521
+ super(props);
522
+ this.state = {
523
+ id: 123,
524
+ };
525
+ }
526
+
527
+ componentDidUpdate(someProps, someState) {
528
+ if (someState.id === someProps.id) {
529
+ doStuff();
530
+ }
531
+ }
532
+ render() {
533
+ return (
534
+ <h1>{this.state.selected ? 'Selected' : 'Not selected'}</h1>
535
+ );
536
+ }
537
+ }` ,
538
+ parser : 'babel-eslint'
539
+ } ,
540
+ {
541
+ code : `class ShouldComponentUpdateTest extends Component {
520
542
constructor(props) {
521
543
super(props);
522
544
this.state = {
@@ -533,6 +555,27 @@ eslintTester.run('no-unused-state', rule, {
533
555
}
534
556
}` ,
535
557
parser : 'babel-eslint'
558
+ } ,
559
+ {
560
+ code : `class NestedScopesTest extends Component {
561
+ constructor(props) {
562
+ super(props);
563
+ this.state = {
564
+ id: 123,
565
+ };
566
+ }
567
+ shouldComponentUpdate(nextProps, nextState) {
568
+ return (function() {
569
+ return nextState.id === nextProps.id;
570
+ })();
571
+ }
572
+ render() {
573
+ return (
574
+ <h1>{this.state.selected ? 'Selected' : 'Not selected'}</h1>
575
+ );
576
+ }
577
+ }` ,
578
+ parser : 'babel-eslint'
536
579
}
537
580
] ,
538
581
@@ -824,6 +867,53 @@ eslintTester.run('no-unused-state', rule, {
824
867
}` ,
825
868
errors : getErrorMessages ( [ 'bar' ] ) ,
826
869
parser : 'babel-eslint'
870
+ } ,
871
+ {
872
+ code : `class FakePrevStateVariableTest extends Component {
873
+ constructor(props) {
874
+ super(props);
875
+ this.state = {
876
+ id: 123,
877
+ foo: 456
878
+ };
879
+ }
880
+
881
+ componentDidUpdate(someProps, someState) {
882
+ if (someState.id === someProps.id) {
883
+ const prevState = { foo: 789 };
884
+ console.log(prevState.foo);
885
+ }
886
+ }
887
+ render() {
888
+ return (
889
+ <h1>{this.state.selected ? 'Selected' : 'Not selected'}</h1>
890
+ );
891
+ }
892
+ }` ,
893
+ errors : getErrorMessages ( [ 'foo' ] ) ,
894
+ parser : 'babel-eslint'
895
+ } ,
896
+ {
897
+ code : `class MissingStateParameterTest extends Component {
898
+ constructor(props) {
899
+ super(props);
900
+ this.state = {
901
+ id: 123
902
+ };
903
+ }
904
+
905
+ componentDidUpdate(someProps) {
906
+ const prevState = { id: 456 };
907
+ console.log(prevState.id);
908
+ }
909
+ render() {
910
+ return (
911
+ <h1>{this.state.selected ? 'Selected' : 'Not selected'}</h1>
912
+ );
913
+ }
914
+ }` ,
915
+ errors : getErrorMessages ( [ 'id' ] ) ,
916
+ parser : 'babel-eslint'
827
917
}
828
918
]
829
919
} ) ;
0 commit comments