Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,23 @@ const allTests = {
`,
errors: [conditionalError('useConditionalHook')],
},
{
code: normalizeIndent`
function Component() {
const items = [1, 2];
items.map(useCustomHook);
}
`,
errors: [hookArgumentError('useCustomHook')],
},
{
code: normalizeIndent`
function Component() {
doSomething(React.useState);
}
`,
errors: [hookArgumentError('React.useState')],
},
{
code: normalizeIndent`
Hook.useState();
Expand Down Expand Up @@ -2062,6 +2079,14 @@ function genericError(hook) {
};
}

function hookArgumentError(hook) {
return {
message:
`React Hook "${hook}" cannot be passed as an argument. React Hooks ` +
'must be called in a React function component or a custom React Hook function.',
};
}

function topLevelError(hook) {
return {
message:
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,25 @@ const rule = {
reactHooks.push(node.callee);
}

if (isInsideComponentOrHook(node)) {
for (const argument of node.arguments) {
if (
argument &&
argument.type !== 'SpreadElement' &&
isHook(argument)
) {
context.report({
node: argument,
message:
`React Hook "${getSourceCode().getText(
argument,
)}" cannot be passed as an argument. React Hooks ` +
'must be called in a React function component or a custom React Hook function.',
});
}
}
}

// useEffectEvent: useEffectEvent functions can be passed by reference within useEffect as well as in
// another useEffectEvent
// Check all `useEffect` and `React.useEffect`, `useEffectEvent`, and `React.useEffectEvent`
Expand Down