mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Warn about conditionally calling hooks when a hook call is an OptionalCallExpression
This commit is contained in:
1 parent
e6fae308e9
commit
2ed38d1885
2 files changed
+19
-1
No files matched your search
@@ -594,6 +594,16 @@ const tests = {
|
||||
`,
|
||||
errors: [conditionalError('useTernaryHook')],
|
||||
},
|
||||
{
|
||||
code: normalizeIndent`
|
||||
// Invalid because it's dangerous and might not warn otherwise.
|
||||
// This *must* be invalid.
|
||||
function ComponentWithOptionalChainingHook() {
|
||||
useHook?.();
|
||||
}
|
||||
`,
|
||||
errors: [conditionalError('useHook')],
|
||||
},
|
||||
{
|
||||
code: normalizeIndent`
|
||||
// Invalid because it's a common misunderstanding.
|
||||
|
||||
@@ -556,7 +556,7 @@ export default {
|
||||
// `CallExpression`s and check that _every use_ of a hook name is valid.
|
||||
// But that gets complicated and enters type-system territory, so we're
|
||||
// only being strict about hook calls for now.
|
||||
CallExpression(node) {
|
||||
'CallExpression, OptionalCallExpression'(node) {
|
||||
if (isHook(node.callee)) {
|
||||
// Add the hook node to a map keyed by the code path segment. We will
|
||||
// do full code path analysis at the end of our code path.
|
||||
@@ -568,6 +568,14 @@ export default {
|
||||
reactHooksMap.set(codePathSegment, reactHooks);
|
||||
}
|
||||
reactHooks.push(node.callee);
|
||||
|
||||
if (node.optional) {
|
||||
const message =
|
||||
`React Hook "${context.getSource(node.callee)}" is called ` +
|
||||
'conditionally. React Hooks must be called in the exact ' +
|
||||
'same order in every component render.';
|
||||
context.report({node, message});
|
||||
}
|
||||
}
|
||||
|
||||
// useEffectEvent: useEffectEvent functions can be passed by reference within useEffect as well as in
|
||||
|
||||
Reference in new issue
Block a user