Bug: Eslint hooks returned by factory functions not linted
See original GitHub issueReact version: *
Steps To Reproduce
See my draft PR with failing tests 👉 #25066.
Given the following code:
// Factory function for creating hooks
function createHooks() {
return {
foo: {
useQuery: () => {
data: 'foo.useQuery'
},
}
};
}
const hooks = createHooks();
export const MyComponent = () => {
if (Math.random() < 0.5) {
return null;
}
// ❌ This should fail the linter
const query = hooks.foo.useQuery();
return <>{query.data}</>;
}
The current behavior
The linting does not catch that the hooks.foo.useQuery()
is used conditionally.
The expected behavior
The linting should catch that the hooks.foo.useQuery()
is used conditionally.
Failing tests / link to code
See my draft PR with failing tests 👉 #25066.
I’ve highlighted areas and things that are up for discussions around this.
Additional context
Partial workaround
If the object returned is not a deep getter, it’s possible to PascalCase
it and do it like this:
// Factory function for creating hooks
function createHooks() {
return {
useFoo: () => {
data: 'foo.useQuery'
};
}
const Hooks = createHooks();
export const MyComponent = () => {
if (Math.random() < 0.5) {
return null;
}
// ✅ This will fail the linter
const query = Hooks.useFoo();
return <>{query.data}</>;
}
It’s okay if hooks can be called outside of React-components
https://github.com/facebook/react/issues/25065#issuecomment-1242767909
Background
I’m the creator of tRPC where we use the following pattern for users to create the root hooks:
// Initialization of the typesafe tRPC hooks
export const trpc = createReactQueryHooks<AppRouter>();
// MyComponent.tsx
export function MyComponent() {
const query = trpc.useQuery(['post.byId', { id: '1' }])
return <pre>{JSON.stringify(query.data ?? null, null, 4)}</pre>
}
In the coming version of tRPC, we are planning on have an API that looks like the below, which also won’t be caught.
export function MyComponent() {
const query = trpc.post.byId.useQury({ id: '1'});
return <pre>{JSON.stringify(query.data ?? null, null, 4)}</pre>
}
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:9
Top Results From Across the Web
func-names - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >axios-hooks
Start using axios-hooks in your project by running `npm i axios-hooks`. There are 60 other projects in the npm registry using axios-hooks.
Read more >typeerror: node.params is not iterable
I get TypeError: node.params is not iterable with eslint command using ... \eslint-plugin-react-hooks\index.js) +9ms eslintrc:config-array-factory Plugin .
Read more >eslint/eslint - Gitter
Get the following error 148:17 error Expected method shorthand object-shorthand . ... (The package "eslint-plugin-react-hook" was not found when loaded as a ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We’re very happy to do a PR to change the behavior to suit our needs, but before doing so I’d love some takes & constraints from the React team so we don’t do a bunch of work in vain that might be rejected regardless.
Sorry, I dropped the ball on this until someone reminded me that this was not solved.
Yes, this would be fine for me too. I’ve updated the description.
Is there anything we can do to enable this behaviour? Would you accept a PR?