Skip to content

Commit 72ecd19

Browse files
committed
fix(no-reset-all-state-on-prop-change): false negatives on custom hooks
Closes #42
1 parent 30a91be commit 72ecd19

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/no-reset-all-state-on-prop-change.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export default {
3535
const effectFnRefs = getEffectFnRefs(context, node);
3636
const depsRefs = getEffectDepsRefs(context, node);
3737
if (!effectFnRefs || !depsRefs) return;
38+
// Skip custom hooks because they can't receive `key` like components can.
39+
const containingNode = findContainingNode(node);
40+
if (containingNode && isCustomHook(containingNode)) return;
3841

3942
const propUsedToResetAllState = findPropUsedToResetAllState(
4043
context,

test/no-reset-all-state-on-prop-change.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ new MyRuleTester().run("no-reset-all-state-on-prop-change", rule, {
6060
}
6161
`,
6262
},
63+
{
64+
name: "Reset all state when a prop changes in a custom hook",
65+
code: js`
66+
function useCustomHook({ userId }) {
67+
const [user, setUser] = useState(null);
68+
const [comment, setComment] = useState('type something');
69+
70+
useEffect(() => {
71+
setUser(null);
72+
setComment('type something');
73+
}, [userId]);
74+
}
75+
`,
76+
},
6377
],
6478
invalid: [
6579
{

0 commit comments

Comments
 (0)