Skip to content

Commit 9519938

Browse files
committed
Add getSnapshotBeforeUpdate support to no-unused-prop-types (fixes #1751)
1 parent d51b0cc commit 9519938

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/rules/no-unused-prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
2323
const DIRECT_NEXT_PROPS_REGEX = /^nextProps\s*(\.|\[)/;
2424
const DIRECT_PREV_PROPS_REGEX = /^prevProps\s*(\.|\[)/;
2525
const LIFE_CYCLE_METHODS = ['componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'componentDidUpdate'];
26-
const ASYNC_SAFE_LIFE_CYCLE_METHODS = ['getDerivedStateFromProps', 'UNSAFE_componentWillReceiveProps', 'UNSAFE_componentWillUpdate'];
26+
const ASYNC_SAFE_LIFE_CYCLE_METHODS = ['getDerivedStateFromProps', 'getSnapshotBeforeUpdate', 'UNSAFE_componentWillReceiveProps', 'UNSAFE_componentWillUpdate'];
2727

2828
// ------------------------------------------------------------------------------
2929
// Rule Definition

tests/lib/rules/no-unused-prop-types.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,26 @@ ruleTester.run('no-unused-prop-types', rule, {
28802880
`].join('\n'),
28812881
settings: {react: {version: '16.3.0'}},
28822882
parser: 'babel-eslint'
2883+
}, {
2884+
// Simple test of new static getSnapshotBeforeUpdate lifecycle
2885+
code: [`
2886+
class MyComponent extends React.Component {
2887+
static propTypes = {
2888+
defaultValue: PropTypes.string
2889+
};
2890+
getSnapshotBeforeUpdate(prevProps, prevState) {
2891+
if (prevProps.defaultValue === null) {
2892+
return 'snapshot';
2893+
}
2894+
return null;
2895+
}
2896+
render() {
2897+
return <div />
2898+
}
2899+
}
2900+
`].join('\n'),
2901+
settings: {react: {version: '16.3.0'}},
2902+
parser: 'babel-eslint'
28832903
}
28842904
],
28852905

@@ -4510,6 +4530,28 @@ ruleTester.run('no-unused-prop-types', rule, {
45104530
errors: [{
45114531
message: '\'defaultValue\' PropType is defined but prop is never used'
45124532
}]
4533+
}, {
4534+
code: [`
4535+
class MyComponent extends React.Component {
4536+
static propTypes = {
4537+
defaultValue: PropTypes.string
4538+
};
4539+
getSnapshotBeforeUpdate(prevProps, prevState) {
4540+
if (prevProps.defaultValue === null) {
4541+
return 'snapshot';
4542+
}
4543+
return null;
4544+
}
4545+
render() {
4546+
return <div />
4547+
}
4548+
}
4549+
`].join('\n'),
4550+
settings: {react: {version: '16.2.0'}},
4551+
parser: 'babel-eslint',
4552+
errors: [{
4553+
message: '\'defaultValue\' PropType is defined but prop is never used'
4554+
}]
45134555
}
45144556

45154557
/* , {

0 commit comments

Comments
 (0)