Skip to content

Commit e0203cb

Browse files
committed
Treat React.useRef/useState/useReducer as static too
1 parent 5d2c991 commit e0203cb

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRuleReactiveDeps-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ const tests = {
281281
const definitelyRef2 = useRef();
282282
const maybeRef1 = useSomeOtherRefyThing();
283283
const [state1, setState1] = useState();
284-
const [state2, setState2] = useState();
284+
const [state2, setState2] = React.useState();
285285
const [state3, dispatch1] = useReducer();
286-
const [state4, dispatch2] = useReducer();
286+
const [state4, dispatch2] = React.useReducer();
287287
const [state5, maybeSetState] = useFunnyState();
288288
const [state6, maybeDispatch] = useFunnyReducer();
289289
function mySetState() {}
@@ -336,9 +336,9 @@ const tests = {
336336
const maybeRef1 = useSomeOtherRefyThing();
337337
338338
const [state1, setState1] = useState();
339-
const [state2, setState2] = useState();
339+
const [state2, setState2] = React.useState();
340340
const [state3, dispatch1] = useReducer();
341-
const [state4, dispatch2] = useReducer();
341+
const [state4, dispatch2] = React.useReducer();
342342
343343
const [state5, maybeSetState] = useFunnyState();
344344
const [state6, maybeDispatch] = useFunnyReducer();

packages/eslint-plugin-react-hooks/src/ReactiveDeps.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,33 @@ export default {
199199
if (def != null && def.node.init != null) {
200200
const init = def.node.init;
201201
if (init.callee != null) {
202+
let callee = init.callee;
202203
if (
203-
init.callee.name === 'useRef' &&
204-
def.node.id.type === 'Identifier'
205-
) {
206-
info.isKnownToBeStatic = true;
207-
} else if (
208-
init.callee.name === 'useState' ||
209-
init.callee.name === 'useReducer'
204+
callee.type === 'MemberExpression' &&
205+
callee.object.name === 'React' &&
206+
callee.property != null
210207
) {
208+
callee = callee.property;
209+
}
210+
if (callee.type === 'Identifier') {
211211
if (
212-
def.node.id.type === 'ArrayPattern' &&
213-
def.node.id.elements.length === 2 &&
214-
Array.isArray(reference.resolved.identifiers) &&
215-
def.node.id.elements[1] ===
216-
reference.resolved.identifiers[0]
212+
callee.name === 'useRef' &&
213+
def.node.id.type === 'Identifier'
217214
) {
218215
info.isKnownToBeStatic = true;
216+
} else if (
217+
callee.name === 'useState' ||
218+
callee.name === 'useReducer'
219+
) {
220+
if (
221+
def.node.id.type === 'ArrayPattern' &&
222+
def.node.id.elements.length === 2 &&
223+
Array.isArray(reference.resolved.identifiers) &&
224+
def.node.id.elements[1] ===
225+
reference.resolved.identifiers[0]
226+
) {
227+
info.isKnownToBeStatic = true;
228+
}
219229
}
220230
}
221231
}

0 commit comments

Comments
 (0)